py/objint_mpz: Fix pow3 where third argument is zero.
This finding is based on fuzzing MicroPython. I manually minimized the test case it provided. Signed-off-by: Jeff Epler <jepler@gmail.com>
This commit is contained in:
committed by
Damien George
parent
ebc9525c95
commit
062e82a7cd
@@ -356,9 +356,10 @@ static mpz_t *mp_mpz_for_int(mp_obj_t arg, mpz_t *temp) {
|
|||||||
mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
|
mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
|
||||||
if (!mp_obj_is_int(base) || !mp_obj_is_int(exponent) || !mp_obj_is_int(modulus)) {
|
if (!mp_obj_is_int(base) || !mp_obj_is_int(exponent) || !mp_obj_is_int(modulus)) {
|
||||||
mp_raise_TypeError(MP_ERROR_TEXT("pow() with 3 arguments requires integers"));
|
mp_raise_TypeError(MP_ERROR_TEXT("pow() with 3 arguments requires integers"));
|
||||||
|
} else if (modulus == MP_OBJ_NEW_SMALL_INT(0)) {
|
||||||
|
mp_raise_ValueError(MP_ERROR_TEXT("divide by zero"));
|
||||||
} else {
|
} else {
|
||||||
mp_obj_t result = mp_obj_new_int_from_ull(0); // Use the _from_ull version as this forces an mpz int
|
mp_obj_int_t *res_p = mp_obj_int_new_mpz();
|
||||||
mp_obj_int_t *res_p = (mp_obj_int_t *)MP_OBJ_TO_PTR(result);
|
|
||||||
|
|
||||||
mpz_t l_temp, r_temp, m_temp;
|
mpz_t l_temp, r_temp, m_temp;
|
||||||
mpz_t *lhs = mp_mpz_for_int(base, &l_temp);
|
mpz_t *lhs = mp_mpz_for_int(base, &l_temp);
|
||||||
@@ -376,7 +377,7 @@ mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
|
|||||||
if (mod == &m_temp) {
|
if (mod == &m_temp) {
|
||||||
mpz_deinit(mod);
|
mpz_deinit(mod);
|
||||||
}
|
}
|
||||||
return result;
|
return MP_OBJ_FROM_PTR(res_p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,3 +20,8 @@ print(hex(pow(2, x-1, x))) # Should be 1, since x is prime
|
|||||||
print(hex(pow(y, x-1, x))) # Should be 1, since x is prime
|
print(hex(pow(y, x-1, x))) # Should be 1, since x is prime
|
||||||
print(hex(pow(y, y-1, x))) # Should be a 'big value'
|
print(hex(pow(y, y-1, x))) # Should be a 'big value'
|
||||||
print(hex(pow(y, y-1, y))) # Should be a 'big value'
|
print(hex(pow(y, y-1, y))) # Should be a 'big value'
|
||||||
|
|
||||||
|
try:
|
||||||
|
print(pow(1, 2, 0))
|
||||||
|
except ValueError:
|
||||||
|
print("ValueError")
|
||||||
|
|||||||
Reference in New Issue
Block a user