Skip to content

Commit 4c4582b

Browse files
committed
objint_mpz: Fix pow3(1,2,0).
This finding is based on fuzzing micropython. I manually minimized the test case it provided. Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent 17fbc5a commit 4c4582b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

py/objint_mpz.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
357357
if (!mp_obj_is_int(base) || !mp_obj_is_int(exponent) || !mp_obj_is_int(modulus)) {
358358
mp_raise_TypeError(MP_ERROR_TEXT("pow() with 3 arguments requires integers"));
359359
} else {
360-
mp_obj_t result = mp_obj_new_int_from_ull(0); // Use the _from_ull version as this forces an mpz int
361-
mp_obj_int_t *res_p = (mp_obj_int_t *)MP_OBJ_TO_PTR(result);
360+
if (modulus == MP_OBJ_NEW_SMALL_INT(0)) {
361+
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("pow() 3rd argument cannot be 0"));
362+
}
363+
mp_obj_int_t *res_p = mp_obj_int_new_mpz();
362364

363365
mpz_t l_temp, r_temp, m_temp;
364366
mpz_t *lhs = mp_mpz_for_int(base, &l_temp);
@@ -376,7 +378,7 @@ mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
376378
if (mod == &m_temp) {
377379
mpz_deinit(mod);
378380
}
379-
return result;
381+
return MP_OBJ_FROM_PTR(res_p);
380382
}
381383
}
382384
#endif

tests/basics/builtin_pow3_intbig.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
print(hex(pow(y, x-1, x))) # Should be 1, since x is prime
2121
print(hex(pow(y, y-1, x))) # Should be a 'big value'
2222
print(hex(pow(y, y-1, y))) # Should be a 'big value'
23+
try:
24+
print(pow(1,2,0))
25+
except ValueError:
26+
print("ValueError")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy