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

bpo-15108: Prevent accessing the result tuple from Python in PySequence_Tuple by pablogsal · Pull Request #24510 · 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_iter.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 @@ -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
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,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
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 @@ -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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL