FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[mypyc] Fix reference leak on invalid byteorder in int.to_bytes (#22236) · python/mypy@97523aa · GitHub

Repository navigation

Commit 97523aa

Browse files
authored
[mypyc] Fix reference leak on invalid byteorder in int.to_bytes (#22236)
Add missing decref in specialization of `int.to_bytes`.
1 parent 4124ade commit 97523aa

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎mypyc/lib-rt/int_ops.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteord
641641
} else if (strcmp(order, "little") == 0) {
642642
little_endian = 1;
643643
} else {
644+
Py_DECREF(pyint);
644645
PyErr_SetString(PyExc_ValueError, "byteorder must be either 'little' or 'big'");
645646
return NULL;
646647
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ class int:
574574
pass
575575

576576
[case testIntToBytes]
577+
import sys
577578
from testutil import assertRaises
578579
def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes:
579580
return n.to_bytes(length, byteorder, signed=signed)
@@ -594,6 +595,19 @@ def test_to_bytes() -> None:
594595
with assertRaises(OverflowError):
595596
to_bytes(128, 1, "big", True)
596597

598+
def test_invalid_byteorder_refcount() -> None:
599+
n = int("123456789012345678901234567890")
600+
before = sys.getrefcount(n)
601+
for _ in range(10):
602+
with assertRaises(ValueError):
603+
to_bytes(n, 16, "invalid")
604+
assert sys.getrefcount(n) == before
605+
# Keep n alive across both refcount checks.
606+
assert n == int("123456789012345678901234567890")
607+
608+
[file sys/__init__.pyi]
609+
def getrefcount(n: object) -> int: ...
610+
597611
[case testBitLength]
598612
def bit_length(n: int) -> int:
599613
return n.bit_length()

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL