Skip to content

Commit c4dafce

Browse files
jeplerdpgeorge
authored andcommitted
py/mpz: Avoid undefined behavior at integer overflow in mpz_hash.
Before this, ubsan would detect a problem when executing hash(006699999999999999999999999999999999999999999999999999999999999999999999) ../../py/mpz.c:1539:20: runtime error: left shift of 1067371580458 by 32 places cannot be represented in type 'mp_int_t' (aka 'long') When the overflow does occur it now happens as defined by the rules of unsigned arithmetic.
1 parent 60eb530 commit c4dafce

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

py/mpz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
15321532

15331533
// must return actual int value if it fits in mp_int_t
15341534
mp_int_t mpz_hash(const mpz_t *z) {
1535-
mp_int_t val = 0;
1535+
mp_uint_t val = 0;
15361536
mpz_dig_t *d = z->dig + z->len;
15371537

15381538
while (d-- > z->dig) {

0 commit comments

Comments
 (0)