Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,16 @@ def test_boundary_error_message_with_negative_offset(self):
'offset -11 out of range for 10-byte buffer'):
struct.pack_into('<B', byte_list, -11, 123)

def test_issue29802(self):
# When the second argument of struct.unpack() was of wrong type
# the Struct object was decrefed twice and the reference to
# deallocated object was left in a cache.
with self.assertRaises(TypeError):
struct.unpack(b'b', 0)
# Shouldn't crash.
self.assertEqual(struct.unpack(b'b', b'a'), (b'a'[0],))


class UnpackIteratorTest(unittest.TestCase):
"""
Tests for iterative unpacking (struct.Struct.iter_unpack).
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ Extension Modules
Library
-------

- bpo-29802: Fixed reference counting in module-level struct functions when
pass arguments of wrong type.

- bpo-30070: Fixed leaks and crashes in errors handling in the parser module.

- bpo-22352: Column widths in the output of dis.dis() are now adjusted for
Expand Down
1 change: 1 addition & 0 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,7 @@ cache_struct_converter(PyObject *fmt, PyObject **ptr)

if (fmt == NULL) {
Py_DECREF(*ptr);
*ptr = NULL;
return 1;
}

Expand Down
1 change: 1 addition & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3907,6 +3907,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
PyObject *output = NULL;
if (arg == NULL) {
Py_DECREF(*(PyObject**)addr);
*(PyObject**)addr = NULL;
return 1;
}

Expand Down