8000 bpo-29802: Fix reference counting in module-level struct functions (#… · python/cpython@40db90c · GitHub
[go: up one dir, main page]

Skip to content

Commit 40db90c

Browse files
bpo-29802: Fix reference counting in module-level struct functions (#1213)
when pass arguments of wrong type.
1 parent 8f5cdfa commit 40db90c

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Lib/test/test_struct.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,16 @@ def test_boundary_error_message_with_negative_offset(self):
599599
'offset -11 out of range for 10-byte buffer'):
600600
struct.pack_into('<B', byte_list, -11, 123)
601601

602+
def test_issue29802(self):
603+
# When the second argument of struct.unpack() was of wrong type
604+
# the Struct object was decrefed twice and the reference to
605+
# deallocated object was left in a cache.
606+
with self.assertRaises(TypeError):
607+
struct.unpack(b'b', 0)
608+
# Shouldn't crash.
609+
self.assertEqual(struct.unpack(b'b', b'a'), (b'a'[0],))
610+
611+
602612
class UnpackIteratorTest(unittest.TestCase):
603613
"""
604614
Tests for iterative unpacking (struct.Struct.iter_unpack).

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ Extension Modules
317317
Library
318318
-------
319319

320+
- bpo-29802: Fixed reference counting in module-level struct functions when
321+
pass arguments of wrong type.
322+
320323
- bpo-30070: Fixed leaks and crashes in errors handling in the parser module.
321324

322325
- bpo-22352: Column widths in the output of dis.dis() are now adjusted for

Modules/_struct.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)
20832083

20842084
if (fmt == NULL) {
20852085
Py_DECREF(*ptr);
2086+
*ptr = NULL;
20862087
return 1;
20872088
}
20882089

Objects/unicodeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3907,6 +3907,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
39073907
PyObject *output = NULL;
39083908
if (arg == NULL) {
39093909
Py_DECREF(*(PyObject**)addr);
3910+
*(PyObject**)addr = NULL;
39103911
return 1;
39113912
}
39123913

0 commit comments

Comments
 (0)
0