Skip to content

Commit d7bb7c7

Browse files
authored
gh-118331: Fix a couple of issues when list allocation fails (#130811)
* Fix use after free in list objects Set the items pointer in the list object to NULL after the items array is freed during list deallocation. Otherwise, we can end up with a list object added to the free list that contains a pointer to an already-freed items array. * Mark `_PyList_FromStackRefStealOnSuccess` as escaping I think technically it's not escaping, because the only object that can be decrefed if allocation fails is an exact list, which cannot execute arbitrary code when it is destroyed. However, this seems less intrusive than trying to special cases objects in the assert in `_Py_Dealloc` that checks for non-null stackpointers and shouldn't matter for performance.
1 parent 2904ec2 commit d7bb7c7

File tree

7 files changed

+26
-4
lines changed

7 files changed

+26
-4
lines changed

Diff for: Include/internal/pycore_opcode_metadata.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Include/internal/pycore_uop_metadata.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Lib/test/test_list.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import sys
2-
from test import list_tests
2+
import textwrap
3+
from test import list_tests, support
34
from test.support import cpython_only
5+
from test.support.import_helper import import_module
6+
from test.support.script_helper import assert_python_failure
47
import pickle
58
import unittest
69

@@ -309,5 +312,20 @@ def test_tier2_invalidates_iterator(self):
309312
a.append(4)
310313
self.assertEqual(list(it), [])
311314

315+
@support.cpython_only
316+
def test_no_memory(self):
317+
# gh-118331: Make sure we don't crash if list allocation fails
318+
import_module("_testcapi")
319+
code = textwrap.dedent("""
320+
import _testcapi, sys
321+
# Prime the freelist
322+
l = [None]
323+
del l
324+
_testcapi.set_nomemory(0)
325+
l = [None]
326+
""")
327+
_, _, err = assert_python_failure("-c", code)
328+
self.assertIn("MemoryError", err.decode("utf-8"))
329+
312330
if __name__ == "__main__":
313331
unittest.main()

Diff for: Objects/listobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ list_dealloc(PyObject *self)
533533
Py_XDECREF(op->ob_item[i]);
534534
}
535535
free_list_items(op->ob_item, false);
536+
op->ob_item = NULL;
536537
}
537538
if (PyList_CheckExact(op)) {
538539
_Py_FREELIST_FREE(lists, op, PyObject_GC_Del);

Diff for: Python/executor_cases.c.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Python/generated_cases.c.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Tools/cases_generator/analyzer.py

-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
632632
"_PyGen_GetGeneratorFromFrame",
633633
"_PyInterpreterState_GET",
634634
"_PyList_AppendTakeRef",
635-
"_PyList_FromStackRefStealOnSuccess",
636635
"_PyList_ITEMS",
637636
"_PyLong_CompactValue",
638637
"_PyLong_DigitCount",

0 commit comments

Comments
 (0)