GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-106168: PyTuple_SET_ITEM() now checks the index by vstinner · Pull Request #106164 · python/cpython

gh-106168: PyTuple_SET_ITEM() now checks the index #106164

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.

Sign up for GitHub

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

Merged
vstinner merged 2 commits into python:main from vstinner:list_get_item_assert
Jun 28, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-106168: PyTuple_SET_ITEM() now checks the index #106164

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
fb5b51b
gh-106168: PyTuple_SET_ITEM() now checks the index
vstinner Jun 27, 2023
9eb147d
Update the doc
vstinner Jun 28, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 4 additions & 0 deletions 4 Doc/c-api/list.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ List Objects
Macro form of :c:func:`PyList_SetItem` without error checking. This is
normally only used to fill in new lists where there is no previous content.

Bounds checking is performed as an assertion if Python is built in
:ref:`debug mode <debug-build>` or :option:`with assertions
<--with-assertions>`.

.. note::

This macro "steals" a reference to *item*, and, unlike
Expand Down
23 changes: 16 additions & 7 deletions 23 Doc/c-api/tuple.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ Tuple Objects
Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
used to fill in brand new tuples.

Bounds checking is performed as an assertion if Python is built in
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.

.. note::

This function "steals" a reference to *o*, and, unlike
Expand Down Expand Up @@ -194,12 +197,17 @@ type.
.. c:function:: PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)

Return the object at position *pos* in the struct sequence pointed to by *p*.
No bounds checking is performed.

Bounds checking is performed as an assertion if Python is built in
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.


.. c:function:: PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)

Macro equivalent of :c:func:`PyStructSequence_GetItem`.
Alias to :c:func:`PyStructSequence_GetItem`.

.. versionchanged:: 3.13
Now implemented as an alias to :c:func:`PyStructSequence_GetItem`.


.. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
Expand All @@ -208,16 +216,17 @@ type.
:c:func:`PyTuple_SET_ITEM`, this should only be used to fill in brand new
instances.

Bounds checking is performed as an assertion if Python is built in
:ref:`debug mode <debug-build>` or :option:`with assertions <--with-assertions>`.

.. note::

This function "steals" a reference to *o*.


.. c:function:: void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)

Similar to :c:func:`PyStructSequence_SetItem`, but implemented as a static
inlined function.
Alias to :c:func:`PyStructSequence_SetItem`.

.. note::

This function "steals" a reference to *o*.
.. versionchanged:: 3.13
Now implemented as an alias to :c:func:`PyStructSequence_SetItem`.
6 changes: 6 additions & 0 deletions 6 Doc/whatsnew/3.13.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ New Features
``NULL`` if the referent is no longer live.
(Contributed by Victor Stinner in :gh:`105927`.)

* If Python is built in :ref:`debug mode <debug-build>` or :option:`with
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion.
If the assertion fails, make sure that the size is set before.
(Contributed by Victor Stinner in :gh:`106168`.)

Porting to Python 3.13
----------------------

Expand Down
2 changes: 2 additions & 0 deletions 2 Include/cpython/listobject.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
static inline void
PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
PyListObject *list = _PyList_CAST(op);
assert(0 <= index);
assert(index < Py_SIZE(list));
list->ob_item[index] = value;
}
#define PyList_SET_ITEM(op, index, value) \
Expand Down
2 changes: 2 additions & 0 deletions 2 Include/cpython/tupleobject.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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(0 <= index);
assert(index < Py_SIZE(tuple));
tuple->ob_item[index] = value;
}
#define PyTuple_SET_ITEM(op, index, value) \
Expand Down
2 changes: 1 addition & 1 deletion 2 Include/internal/pycore_list.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
Py_ssize_t allocated = self->allocated;
assert((size_t)len + 1 < PY_SSIZE_T_MAX);
if (allocated > len) {
PyList_SET_ITEM(self, len, newitem);
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, newitem);
return 0;
}
return _PyList_AppendTakeRefListResize(self, newitem);
Expand Down
13 changes: 5 additions & 8 deletions 13 Include/structseq.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ PyAPI_FUNC(PyTypeObject*) PyStructSequence_NewType(PyStructSequence_Desc *desc);

PyAPI_FUNC(PyObject *) PyStructSequence_New(PyTypeObject* type);

PyAPI_FUNC(void) PyStructSequence_SetItem(PyObject*, Py_ssize_t, PyObject*);
PyAPI_FUNC(PyObject*) PyStructSequence_GetItem(PyObject*, Py_ssize_t);

#ifndef Py_LIMITED_API
typedef PyTupleObject PyStructSequence;

/* Macro, *only* to be used to fill in brand new objects */
#define PyStructSequence_SET_ITEM(op, i, v) PyTuple_SET_ITEM((op), (i), (v))

#define PyStructSequence_GET_ITEM(op, i) PyTuple_GET_ITEM((op), (i))
#define PyStructSequence_SET_ITEM PyStructSequence_SetItem
#define PyStructSequence_GET_ITEM PyStructSequence_GetItem
#endif

PyAPI_FUNC(void) PyStructSequence_SetItem(PyObject*, Py_ssize_t, PyObject*);
PyAPI_FUNC(PyObject*) PyStructSequence_GetItem(PyObject*, Py_ssize_t);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions 5 Misc/NEWS.d/next/C API/2023-06-28-02-30-50.gh-issue-106168.NFOZPv.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
If Python is built in :ref:`debug mode <debug-build>` or :option:`with
assertions <--with-assertions>`, :c:func:`PyTuple_SET_ITEM` and
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion. If
the assertion fails, make sure that the size is set before. Patch by Victor
Stinner.
5 changes: 3 additions & 2 deletions 5 Objects/listobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,9 @@ list_extend(PyListObject *self, PyObject *iterable)
}
if (Py_SIZE(self) < self->allocated) {
/* steals ref */
PyList_SET_ITEM(self, Py_SIZE(self), item);
Py_SET_SIZE(self, Py_SIZE(self) + 1);
Py_ssize_t len = Py_SIZE(self);
Py_SET_SIZE(self, len + 1);
PyList_SET_ITEM(self, len, item);
}
else {
if (_PyList_AppendTakeRef(self, item) < 0)
Expand Down
23 changes: 18 additions & 5 deletions 23 Objects/structseq.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,28 @@ PyStructSequence_New(PyTypeObject *type)
}

void
PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v)
PyStructSequence_SetItem(PyObject *op, Py_ssize_t index, PyObject *value)
{
PyStructSequence_SET_ITEM(op, i, v);
PyTupleObject *tuple = _PyTuple_CAST(op);
assert(0 <= index);
#ifndef NDEBUG
Py_ssize_t n_fields = REAL_SIZE(op);
assert(n_fields >= 0);
assert(index < n_fields);
#endif
tuple->ob_item[index] = value;
}

PyObject*
PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
PyStructSequence_GetItem(PyObject *op, Py_ssize_t index)
{
return PyStructSequence_GET_ITEM(op, i);
assert(0 <= index);
#ifndef NDEBUG
Py_ssize_t n_fields = REAL_SIZE(op);
assert(n_fields >= 0);
assert(index < n_fields);
#endif
return PyTuple_GET_ITEM(op, index);
}


Expand Down Expand Up @@ -287,7 +300,7 @@ structseq_repr(PyStructSequence *obj)
goto error;
}

PyObject *value = PyStructSequence_GET_ITEM(obj, i);
PyObject *value = PyStructSequence_GetItem((PyObject*)obj, i);
assert(value != NULL);
PyObject *repr = PyObject_Repr(value);
if (repr == NULL) {
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.