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

gh-148653: Fix SIGSEGV in marshal.loads for self-referencing tuples by mjbommar · Pull Request #148652 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 20 additions & 0 deletions Lib/test/test_marshal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -863,5 +863,25 @@ def test_read_object_from_file(self):
os_helper.unlink(os_helper.TESTFN)


class SelfRefTupleTest(unittest.TestCase):
"""Regression test for gh-148653: TYPE_TUPLE with FLAG_REF back-reference.

R_REF registered the tuple in p->refs before its slots were populated.
A TYPE_REF back-reference to the partial tuple could reach a hashing
site (PySet_Add) with NULL slots, crashing with SIGSEGV.

The fix uses the two-phase r_ref_reserve/r_ref_insert pattern so the
Py_None placeholder is detected by the TYPE_REF handler, raising
ValueError instead.
"""

def test_self_ref_tuple(self):
# TYPE_TUPLE|FLAG_REF n=2; NONE; TYPE_SET n=1; TYPE_REF(0)
payload = (b'\xa8\x02\x00\x00\x00N'
b'<\x01\x00\x00\x00r\x00\x00\x00\x00')
with self.assertRaises(ValueError):
marshal.loads(payload)


if __name__ == "__main__":
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix SIGSEGV in :func:`marshal.loads` for self-referencing tuples. Such
payloads will now raise :exc:`ValueError` instead of crashing.
5 changes: 4 additions & 1 deletion Python/marshal.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,9 @@ r_object(RFILE *p)
}
_read_tuple:
v = PyTuple_New(n);
R_REF(v);
idx = r_ref_reserve(flag, p);
if (idx < 0)
Py_CLEAR(v);
if (v == NULL)
break;

Expand All @@ -1433,6 +1435,7 @@ r_object(RFILE *p)
}
PyTuple_SET_ITEM(v, i, v2);
}
v = r_ref_insert(v, idx, flag, p);
retval = v;
break;

Expand Down
Loading

Back | FazBrowse Home | New Git URL