Skip to content

Commit ae57605

Browse files
[3.14] gh-106287: Do not write objects after an unmarshalling error (GH-132715) (GH-143832)
Writing out an object may involve a slot lookup, which is not safe to do with an exception raised. In debug mode an assertion failure will occur if this happens. (cherry picked from commit ce8f5f9) Co-authored-by: Duane Griffin <duaneg@dghda.com>
1 parent a36ba5a commit ae57605

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Lib/test/test_marshal.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,26 @@ def test_deterministic_sets(self):
413413
_, dump_1, _ = assert_python_ok(*args, PYTHONHASHSEED="1")
414414
self.assertEqual(dump_0, dump_1)
415415

416+
def test_unmarshallable(self):
417+
# Check no crash after encountering unmarshallable objects.
418+
# See https://github.com/python/cpython/issues/106287.
419+
fset = frozenset([int])
420+
code = compile("a = 1", "<string>", "exec")
421+
code = code.replace(co_consts=(1, fset, None))
422+
cases = (('tuple', (fset,)),
423+
('list', [fset]),
424+
('set', fset),
425+
('dict key', {fset: 'x'}),
426+
('dict value', {'x': fset}),
427+
('dict key & value', {fset: fset}),
428+
('slice', slice(fset, fset)),
429+
('code', code))
430+
for name, arg in cases:
431+
with self.subTest(name, arg=arg):
432+
with self.assertRaisesRegex(ValueError, "unmarshallable object"):
433+
marshal.dumps((arg, memoryview(b'')))
434+
435+
416436
LARGE_SIZE = 2**31
417437
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
418438

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip writing objects during marshalling once a failure has occurred.

Python/marshal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ w_object(PyObject *v, WFILE *p)
432432
{
433433
char flag = '\0';
434434

435+
if (p->error != WFERR_OK) {
436+
return;
437+
}
438+
435439
p->depth++;
436440

437441
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {

0 commit comments

Comments
 (0)