Skip to content

Commit c447889

Browse files
committed
Catch overflow in MPZ_to_bytes()
1 parent fd5395b commit c447889

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,9 @@ MPZ_to_bytes(MPZ_Object *u, Py_ssize_t length, int is_little, int is_signed)
20092009
return NULL;
20102010
/* LCOV_EXCL_STOP */
20112011
}
2012+
if (tmp->size < u->size) {
2013+
goto overflow;
2014+
}
20122015
mpn_zero(tmp->digits, tmp->size);
20132016
tmp->digits[tmp->size - 1] = 1;
20142017
tmp->digits[tmp->size - 1] <<= (8*length) % (GMP_NUMB_BITS*tmp->size);
@@ -2023,6 +2026,7 @@ MPZ_to_bytes(MPZ_Object *u, Py_ssize_t length, int is_little, int is_signed)
20232026
|| (is_signed && nbits
20242027
&& (nbits == 8 * length ? !is_negative : is_negative)))
20252028
{
2029+
overflow:
20262030
Py_XDECREF(tmp);
20272031
PyErr_SetString(PyExc_OverflowError, "int too big to convert");
20282032
return NULL;

0 commit comments

Comments
 (0)