Skip to content

Commit cd8b2ba

Browse files
committed
py: Fix bug in mpz int, where small int is on lhs, mpz on rhs.
1 parent 494600b commit cd8b2ba

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

py/objint_mpz.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
4444
}
4545

4646
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
47-
mpz_t *zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
47+
const mpz_t *zlhs;
4848
const mpz_t *zrhs;
4949
mpz_t z_int;
5050
mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
5151

52+
// lhs could be a small int (eg small-int + mpz)
53+
if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
54+
mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
55+
zlhs = &z_int;
56+
} else if (MP_OBJ_IS_TYPE(lhs_in, &int_type)) {
57+
zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
58+
} else {
59+
return MP_OBJ_NULL;
60+
}
61+
62+
// if rhs is small int, then lhs was not (otherwise rt_binary_op handles it)
5263
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
5364
mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
5465
zrhs = &z_int;

0 commit comments

Comments
 (0)