From c4dafcef4fe9edaacaeeb16c412c298cbab3b414 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 19 May 2018 11:20:29 -0500 Subject: [PATCH] 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. --- py/mpz.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/mpz.c b/py/mpz.c index fa50868620..8687092d02 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -1532,7 +1532,7 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) { // must return actual int value if it fits in mp_int_t mp_int_t mpz_hash(const mpz_t *z) { - mp_int_t val = 0; + mp_uint_t val = 0; mpz_dig_t *d = z->dig + z->len; while (d-- > z->dig) {