Skip to content

bpo-15108: Prevent accessing the result tuple from Python in PySequence_Tuple #24510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions Lib/test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import unittest
import gc
from test.support import run_unittest, cpython_only
from test.support.os_helper import TESTFN, unlink
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
Expand Down Expand Up @@ -1036,6 +1037,25 @@ def test_error_iter(self):
self.assertRaises(TypeError, iter, typ())
self.assertRaises(ZeroDivisionError, iter, BadIterableClass())

def test_access_result_tuple_while_iterating(self):
TAG = object()

def monitor():
lst = [x for x in gc.get_referrers(TAG) if isinstance(x, tuple)]
# This would be the result tuple if is accessible mid-iteration
t = lst[0]
print(t)
return t

def my_iter():
yield TAG
t = monitor()
breakpoint()
for x in range(10):
yield x

self.assertRaises(IndexError, tuple, my_iter())


def test_main():
run_unittest(TestCase)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed an issue that could cause a crash when accessing an incomplete tuple
when collecting an iterable into a :class:`tuple` using :c:func:`PySequence_Tuple`.
Patch by Pablo Galindo.
10 changes: 10 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,9 @@ PySequence_Tuple(PyObject *v)
if (result == NULL)
goto Fail;

// bpo-15108: Code can access the result tuple while being
// incomplete when calling PyIter_Next().
PyObject_GC_UnTrack(result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to add a new API to create an untracked tuple: https://bugs.python.org/issue15108#msg387029

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss int the issue, then I can modify the PR :)

/* Fill the tuple. */
for (j = 0; ; ++j) {
PyObject *item = PyIter_Next(it);
Expand Down Expand Up @@ -2022,10 +2025,17 @@ PySequence_Tuple(PyObject *v)
Py_DECREF(item);
goto Fail;
}
// Resizing could track the tuple again
PyObject_GC_UnTrack(result);
}
PyTuple_SET_ITEM(result, j, item);
}

// No more calls can go back into Python, so is safe
// to re-track the tuple.

PyObject_GC_Track(result);

/* Cut tuple back if guess was too large. */
if (j < n &&
_PyTuple_Resize(&result, j) != 0)
Expand Down