1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

mpint.c: outlaw mp_ints with nw==0.

Some functions got confused if given one as input (particularly
mp_get_decimal, which assumed it could safely write at least one word
into the inv5 value it makes internally), and I've decided it's easier
to stop them ever being created than to teach everything to handle
them correctly. So now mp_make_sized enforces nw != 0 by assertion,
and I've added a max at any call site that looked as if it might
violate that precondition.

mp_from_hex("") could generate one of these, in particular, so now
I've fixed it, I've added a test to make sure it continues doing
something sensible.
This commit is contained in:
Simon Tatham
2019-01-29 20:03:28 +00:00
parent 9e6669d30a
commit 5017d0a6ca
2 changed files with 12 additions and 6 deletions

View File

@ -159,6 +159,7 @@ class mpint(MyTestBase):
hexstr = 'ea7cb89f409ae845215822e37D32D0C63EC43E1381C2FF8094'
self.assertEqual(int(mp_from_hex_pl(hexstr)), int(hexstr, 16))
self.assertEqual(int(mp_from_hex(hexstr)), int(hexstr, 16))
self.assertEqual(int(mp_from_hex("")), 0)
p2 = mp_power_2(123)
self.assertEqual(int(p2), 1 << 123)
p2c = mp_copy(p2)
@ -319,7 +320,7 @@ class mpint(MyTestBase):
diff = mp_sub(am, bm)
self.assertEqual(int(diff), (ai - bi) & mp_mask(diff))
for bits in range(0, 512, 64):
for bits in range(64, 512, 64):
cm = mp_new(bits)
mp_add_into(cm, am, bm)
self.assertEqual(int(cm), (ai + bi) & mp_mask(cm))
@ -357,8 +358,8 @@ class mpint(MyTestBase):
if r >= d:
continue # silly cases with tiny divisors
n = q*d + r
mq = mp_new(nbits(q))
mr = mp_new(nbits(r))
mq = mp_new(max(nbits(q), 1))
mr = mp_new(max(nbits(r), 1))
mp_divmod_into(n, d, mq, mr)
self.assertEqual(int(mq), q)
self.assertEqual(int(mr), r)