Repository navigation
GH-59313: Do not allow incomplete tuples, or tuples with NULLs. #117747
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
Changes from 5 commits
d4d6f24
6a1f806
9aeb6da
40a49d6
0a06553
207005c
03643cf
0d8974e
8fad5a4
e4b4f39
f4c0a29
bf69fc5
4d0ad9e
269832a
3a766d8
003264f
a155802
ae437f1
03ce121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) { | |
| static inline void | ||
| PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) { | ||
| PyTupleObject *tuple = _PyTuple_CAST(op); | ||
| assert(value != NULL); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You modified PyTuple_SET_ITEM() but not PyTuple_SetItem(), is it on purpose? I suggest to modify both, since PyTuple_SetItem() doesn't call PyTuple_SET_ITEM().
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| assert(0 <= index); | ||
| assert(index < Py_SIZE(tuple)); | ||
| tuple->ob_item[index] = value; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -416,6 +416,28 @@ def test_lexicographic_ordering(self): | |
| self.assertLess(a, b) | ||
| self.assertLess(b, c) | ||
|
|
||
| def test_bug_59313(self): | ||
| # Until 3.13, the C-API function PySequence_Tuple | ||
| # would create incomplete tuples which were visible | ||
| # to the cycle GC | ||
| TAG = object() | ||
|
|
||
| def monitor(): | ||
| lst = [x for x in gc.get_referrers(TAG) | ||
| if isinstance(x, tuple)] | ||
| t = lst[0] # this *is* the result tuple | ||
| print(t) # full of nulls ! | ||
| return t # Keep it alive for some time | ||
|
|
||
| def my_iter(): | ||
| yield TAG # 'tag' gets stored in the result tuple | ||
| t = monitor() | ||
| for x in range(10): | ||
| yield x # SystemError when the tuple needs to be resized | ||
|
|
||
| with self.assertRaises(IndexError): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to rewrite the test to return somehow Example: def my_iter():
yield TAG # 'tag' gets stored in the result tuple
yield [x for x in gc.get_referrers(TAG) if isinstance(x, tuple)]
for x in range(10):
yield x
result = tuple(my_iter())
self.assertEqual(result, (TAG, [], *range(10)))
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| tuple(my_iter()) | ||
|
|
||
| # Notes on testing hash codes. The primary thing is that Python doesn't | ||
| # care about "random" hash codes. To the contrary, we like them to be | ||
| # very regular when possible, so that the low-order bits are as evenly | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Make sure that all tuple elements are valid and never NULL. Ensuring that | ||
| tuples are always valid reduces the risk of cycle GC bugs and prevents | ||
| ``gc.get_referrers`` from returning invalid tuples. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2040,8 +2040,6 @@ PySequence_Tuple(PyObject *v) | |
| { | ||
| PyObject *it; /* iter(v) */ | ||
| Py_ssize_t n; /* guess for result tuple size */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a warning: unused variable ‘n’ [-Wunused-variable]
erlend-aasland marked this conversation as resolved.
Outdated
|
||
| PyObject *result = NULL; | ||
| Py_ssize_t j; | ||
|
|
||
| if (v == NULL) { | ||
| return null_error(); | ||
|
|
@@ -2067,53 +2065,31 @@ PySequence_Tuple(PyObject *v) | |
| n = PyObject_LengthHint(v, 10); | ||
| if (n == -1) | ||
| goto Fail; | ||
| result = PyTuple_New(n); | ||
| if (result == NULL) | ||
| PyObject *temp = PyList_New(0); | ||
| if (temp == NULL) | ||
| goto Fail; | ||
|
|
||
| /* Fill the tuple. */ | ||
| for (j = 0; ; ++j) { | ||
| /* Fill the temporary list. */ | ||
| for (;;) { | ||
| PyObject *item = PyIter_Next(it); | ||
| if (item == NULL) { | ||
| if (PyErr_Occurred()) | ||
| if (PyErr_Occurred()) { | ||
| Py_DECREF(temp); | ||
| goto Fail; | ||
| } | ||
| break; | ||
| } | ||
| if (j >= n) { | ||
| size_t newn = (size_t)n; | ||
| /* The over-allocation strategy can grow a bit faster | ||
| than for lists because unlike lists the | ||
| over-allocation isn't permanent -- we reclaim | ||
| the excess before the end of this routine. | ||
| So, grow by ten and then add 25%. | ||
| */ | ||
| newn += 10u; | ||
| newn += newn >> 2; | ||
| if (newn > PY_SSIZE_T_MAX) { | ||
| /* Check for overflow */ | ||
| PyErr_NoMemory(); | ||
| Py_DECREF(item); | ||
| goto Fail; | ||
| } | ||
| n = (Py_ssize_t)newn; | ||
| if (_PyTuple_Resize(&result, n) != 0) { | ||
| Py_DECREF(item); | ||
| goto Fail; | ||
| } | ||
| if (_PyList_AppendTakeRef((PyListObject *)temp, item)) { | ||
| Py_DECREF(temp); | ||
| goto Fail; | ||
| } | ||
| PyTuple_SET_ITEM(result, j, item); | ||
| } | ||
|
|
||
| /* Cut tuple back if guess was too large. */ | ||
| if (j < n && | ||
| _PyTuple_Resize(&result, j) != 0) | ||
| goto Fail; | ||
|
|
||
| Py_DECREF(it); | ||
| PyObject *result = _PyList_AsTupleStealItems(temp); | ||
| Py_DECREF(temp); | ||
| return result; | ||
|
|
||
| Fail: | ||
| Py_XDECREF(result); | ||
| Py_DECREF(it); | ||
| return NULL; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,14 +34,7 @@ static inline int maybe_freelist_push(PyTupleObject *); | |
| static PyTupleObject * | ||
| tuple_alloc(Py_ssize_t size) | ||
| { | ||
| if (size < 0) { | ||
| PyErr_BadInternalCall(); | ||
| return NULL; | ||
| } | ||
| #ifdef Py_DEBUG | ||
| assert(size != 0); // The empty tuple is statically allocated. | ||
| #endif | ||
|
|
||
| assert(size > 0); | ||
| PyTupleObject *op = maybe_freelist_pop(size); | ||
| if (op == NULL) { | ||
| /* Check for overflow */ | ||
|
|
@@ -78,7 +71,7 @@ PyTuple_New(Py_ssize_t size) | |
| return NULL; | ||
| } | ||
| for (Py_ssize_t i = 0; i < size; i++) { | ||
| op->ob_item[i] = NULL; | ||
| op->ob_item[i] = Py_None; | ||
| } | ||
| _PyObject_GC_TRACK(op); | ||
| return (PyObject *) op; | ||
|
|
@@ -205,7 +198,8 @@ tupledealloc(PyTupleObject *op) | |
|
|
||
| Py_ssize_t i = Py_SIZE(op); | ||
| while (--i >= 0) { | ||
| Py_XDECREF(op->ob_item[i]); | ||
| assert(op->ob_item[i] != NULL); | ||
| Py_DECREF(op->ob_item[i]); | ||
| } | ||
| // This will abort on the empty singleton (if there is one). | ||
| if (!maybe_freelist_push(op)) { | ||
|
|
@@ -391,11 +385,8 @@ _PyTuple_FromArray(PyObject *const *src, Py_ssize_t n) | |
| } | ||
|
|
||
| PyObject * | ||
| _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n) | ||
| _PyTuple_FromNonEmptyArraySteal(PyObject *const *src, Py_ssize_t n) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how this change is related to the fix. And I don't see the benefits to reject n=0.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've reverted it.
|
||
| { | ||
| if (n == 0) { | ||
| return tuple_get_empty(); | ||
| } | ||
| PyTupleObject *tuple = tuple_alloc(n); | ||
| if (tuple == NULL) { | ||
| for (Py_ssize_t i = 0; i < n; i++) { | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.