Skip to content

Commit 9c626cf

Browse files
authored
[mypyc] Propagate errors and fix reference leaks in integer conversion (#22321)
Propagate error when str-to-int conversion with explicit base fails on very large bases. Propagate error and fix reference leak when int-to-bytes conversion on large ints fails when converting it to str. Fix similar reference leak during the int-to-str conversion.
1 parent b3ab06a commit 9c626cf

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎mypyc/lib-rt/int_ops.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right) {
225225

226226
PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base) {
227227
Py_ssize_t base_size_t = CPyTagged_AsSsize_t(base);
228+
if (base_size_t == -1 && PyErr_Occurred()) {
229+
return NULL;
230+
}
228231
return PyLong_FromUnicodeObject(o, base_size_t);
229232
}
230233

‎mypyc/lib-rt/misc_ops.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,10 @@ PyObject *CPyTagged_Str(CPyTagged n) {
586586
if (CPyTagged_CheckShort(n)) {
587587
return CPyTagged_ShortToStr(CPyTagged_ShortAsSsize_t(n));
588588
} else {
589-
return PyObject_Str(CPyTagged_AsObject(n));
589+
PyObject *obj = CPyTagged_AsObject(n);
590+
PyObject *str = PyObject_Str(obj);
591+
Py_DECREF(obj);
592+
return str;
590593
}
591594
}
592595

@@ -602,7 +605,12 @@ PyObject *CPyTagged_AsciiBytes(CPyTagged n) {
602605
if (CPyTagged_CheckShort(n)) {
603606
return CPyTagged_ShortToAsciiBytes(CPyTagged_ShortAsSsize_t(n));
604607
}
605-
PyObject *str = PyObject_Str(CPyTagged_AsObject(n));
608+
PyObject *obj = CPyTagged_AsObject(n);
609+
PyObject *str = PyObject_Str(obj);
610+
Py_DECREF(obj);
611+
if (str == NULL) {
612+
return NULL;
613+
}
606614
PyObject *bytes = PyUnicode_AsASCIIString(str);
607615
CPy_DECREF(str);
608616
return bytes;

‎mypyc/test-data/run-bytes.test‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,30 @@ class bytes_subclass(bytes):
579579
return b'spook'
580580

581581
[case testBytesFormatting]
582+
import sys
583+
from typing import Any
582584
from testutil import assertRaises
583585

586+
def format_int(n: int) -> bytes:
587+
return b"%d" % n
588+
589+
def test_bytes_formatting_error_propagation() -> None:
590+
sys_module: Any = sys
591+
if hasattr(sys_module, "set_int_max_str_digits"):
592+
old_limit = sys_module.get_int_max_str_digits()
593+
try:
594+
sys_module.set_int_max_str_digits(640)
595+
n = 10 ** 1000
596+
before = sys_module.getrefcount(n)
597+
for _ in range(3):
598+
with assertRaises(ValueError):
599+
format_int(n)
600+
assert sys_module.getrefcount(n) == before
601+
assert n == 10 ** 1000 # Keep n alive across both refcount checks.
602+
finally:
603+
sys_module.set_int_max_str_digits(old_limit)
604+
assert format_int(123) == b"123"
605+
584606
# https://www.python.org/dev/peps/pep-0461/
585607
def test_bytes_formatting() -> None:
586608
val = 10

‎mypyc/test-data/run-integers.test‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,15 @@ def test_true_divide() -> None:
490490
huge / -2
491491
assert 1 / huge == 0.0
492492

493+
def from_base(s: str, base: int) -> int:
494+
return int(s, base)
495+
496+
def test_base_overflow() -> None:
497+
for base in (2 ** 100, -(2 ** 100)):
498+
with assertRaises(OverflowError):
499+
from_base("10", base)
500+
assert from_base("10", 10) == 10
501+
493502
[case testIntMinMax]
494503
def test_int_min_max() -> None:
495504
x: int = 200

0 commit comments

Comments
 (0)