Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb5b51b

Browse files
committed
gh-106168: PyTuple_SET_ITEM() now checks the index
PyTuple_SET_ITEM() and PyList_SET_ITEM() now check the index argumentwith an assertion if Python is built in debug mode or is built withassertions.* list_extend() and _PyList_AppendTakeRef() now set the list size before calling PyList_SET_ITEM().* PyStructSequence_GetItem() and PyStructSequence_SetItem() now check the index argument: must be lesser than REAL_SIZE(op).* PyStructSequence_GET_ITEM() and PyStructSequence_SET_ITEM() are now aliases to PyStructSequence_GetItem() and PyStructSequence_SetItem().
1 parent84caa33 commitfb5b51b

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ New Features
441441
``NULL`` if the referent is no longer live.
442442
(Contributed by Victor Stinner in:gh:`105927`.)
443443

444+
* If Python is built in:ref:`debug mode<debug-build>` or:option:`with
445+
assertions <--with-assertions>`,:c:func:`PyTuple_SET_ITEM` and
446+
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion.
447+
If the assertion fails, make sure that the size is set before.
448+
(Contributed by Victor Stinner in:gh:`106168`.)
449+
444450
Porting to Python 3.13
445451
----------------------
446452

‎Include/cpython/listobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
4141
staticinlinevoid
4242
PyList_SET_ITEM(PyObject*op,Py_ssize_tindex,PyObject*value) {
4343
PyListObject*list=_PyList_CAST(op);
44+
assert(0 <=index);
45+
assert(index<Py_SIZE(list));
4446
list->ob_item[index]=value;
4547
}
4648
#definePyList_SET_ITEM(op,index,value) \

‎Include/cpython/tupleobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
3131
staticinlinevoid
3232
PyTuple_SET_ITEM(PyObject*op,Py_ssize_tindex,PyObject*value) {
3333
PyTupleObject*tuple=_PyTuple_CAST(op);
34+
assert(0 <=index);
35+
assert(index<Py_SIZE(tuple));
3436
tuple->ob_item[index]=value;
3537
}
3638
#definePyTuple_SET_ITEM(op,index,value) \

‎Include/internal/pycore_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
4949
Py_ssize_tallocated=self->allocated;
5050
assert((size_t)len+1<PY_SSIZE_T_MAX);
5151
if (allocated>len) {
52-
PyList_SET_ITEM(self,len,newitem);
5352
Py_SET_SIZE(self,len+1);
53+
PyList_SET_ITEM(self,len,newitem);
5454
return0;
5555
}
5656
return_PyList_AppendTakeRefListResize(self,newitem);

‎Include/structseq.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ PyAPI_FUNC(PyTypeObject*) PyStructSequence_NewType(PyStructSequence_Desc *desc);
3131

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

34+
PyAPI_FUNC(void)PyStructSequence_SetItem(PyObject*,Py_ssize_t,PyObject*);
35+
PyAPI_FUNC(PyObject*)PyStructSequence_GetItem(PyObject*,Py_ssize_t);
36+
3437
#ifndefPy_LIMITED_API
3538
typedefPyTupleObjectPyStructSequence;
36-
37-
/* Macro, *only* to be used to fill in brand new objects */
38-
#definePyStructSequence_SET_ITEM(op,i,v) PyTuple_SET_ITEM((op), (i), (v))
39-
40-
#definePyStructSequence_GET_ITEM(op,i) PyTuple_GET_ITEM((op), (i))
39+
#definePyStructSequence_SET_ITEM PyStructSequence_SetItem
40+
#definePyStructSequence_GET_ITEM PyStructSequence_GetItem
4141
#endif
4242

43-
PyAPI_FUNC(void)PyStructSequence_SetItem(PyObject*,Py_ssize_t,PyObject*);
44-
PyAPI_FUNC(PyObject*)PyStructSequence_GetItem(PyObject*,Py_ssize_t);
45-
4643
#ifdef__cplusplus
4744
}
4845
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
If Python is built in:ref:`debug mode<debug-build>` or:option:`with
2+
assertions <--with-assertions>`,:c:func:`PyTuple_SET_ITEM` and
3+
:c:func:`PyList_SET_ITEM` now check the index argument with an assertion. If
4+
the assertion fails, make sure that the size is set before. Patch by Victor
5+
Stinner.

‎Objects/listobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,9 @@ list_extend(PyListObject *self, PyObject *iterable)
953953
}
954954
if (Py_SIZE(self)<self->allocated) {
955955
/* steals ref */
956-
PyList_SET_ITEM(self,Py_SIZE(self),item);
957-
Py_SET_SIZE(self,Py_SIZE(self)+1);
956+
Py_ssize_tlen=Py_SIZE(self);
957+
Py_SET_SIZE(self,len+1);
958+
PyList_SET_ITEM(self,len,item);
958959
}
959960
else {
960961
if (_PyList_AppendTakeRef(self,item)<0)

‎Objects/structseq.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,28 @@ PyStructSequence_New(PyTypeObject *type)
7474
}
7575

7676
void
77-
PyStructSequence_SetItem(PyObject*op,Py_ssize_ti,PyObject*v)
77+
PyStructSequence_SetItem(PyObject*op,Py_ssize_tindex,PyObject*value)
7878
{
79-
PyStructSequence_SET_ITEM(op,i,v);
79+
PyTupleObject*tuple=_PyTuple_CAST(op);
80+
assert(0 <=index);
81+
#ifndefNDEBUG
82+
Py_ssize_tn_fields=REAL_SIZE(op);
83+
assert(n_fields >=0);
84+
assert(index<n_fields);
85+
#endif
86+
tuple->ob_item[index]=value;
8087
}
8188

8289
PyObject*
83-
PyStructSequence_GetItem(PyObject*op,Py_ssize_ti)
90+
PyStructSequence_GetItem(PyObject*op,Py_ssize_tindex)
8491
{
85-
returnPyStructSequence_GET_ITEM(op,i);
92+
assert(0 <=index);
93+
#ifndefNDEBUG
94+
Py_ssize_tn_fields=REAL_SIZE(op);
95+
assert(n_fields >=0);
96+
assert(index<n_fields);
97+
#endif
98+
returnPyTuple_GET_ITEM(op,index);
8699
}
87100

88101

@@ -287,7 +300,7 @@ structseq_repr(PyStructSequence *obj)
287300
gotoerror;
288301
}
289302

290-
PyObject*value=PyStructSequence_GET_ITEM(obj,i);
303+
PyObject*value=PyStructSequence_GetItem((PyObject*)obj,i);
291304
assert(value!=NULL);
292305
PyObject*repr=PyObject_Repr(value);
293306
if (repr==NULL) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp