Skip to content

Commit e83f140

Browse files
committed
py/mpz: Remove unreachable code in mpn_or_neg functions.
1 parent 9112b0b commit e83f140

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

py/mpz.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,13 @@ STATIC mp_uint_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jl
308308
carryi >>= DIG_SIZE;
309309
}
310310

311-
if (0 != carryi) {
312-
*idig++ = carryi;
313-
}
311+
// At least one of j,k must be negative so the above for-loop runs at least
312+
// once. For carryi to be non-zero here it must be equal to 1 at the end of
313+
// each iteration of the loop. So the accumulation of carryi must overflow
314+
// each time, ie carryi += 0xff..ff. So carryj|carryk must be 0 in the
315+
// DIG_MASK bits on each iteration. But considering all cases of signs of
316+
// j,k one sees that this is not possible.
317+
assert(carryi == 0);
314318

315319
return mpn_remove_trailing_zeros(oidig, idig);
316320
}
@@ -334,9 +338,8 @@ STATIC mp_uint_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jl
334338
carryi >>= DIG_SIZE;
335339
}
336340

337-
if (0 != carryi) {
338-
*idig++ = carryi;
339-
}
341+
// See comment in above mpn_or_neg for why carryi must be 0.
342+
assert(carryi == 0);
340343

341344
return mpn_remove_trailing_zeros(oidig, idig);
342345
}

0 commit comments

Comments
 (0)