Skip to content

Do not DECREF tuple until tuple items are no longer used#9707

Open
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:decref
Open

Do not DECREF tuple until tuple items are no longer used#9707
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:decref

Conversation

@radarhere

Copy link
Copy Markdown
Member

Pillow/src/encode.c

Lines 767 to 777 in 2c3a977

item = PyList_GetItemRef(tags, pos);
if (item == NULL) {
Py_DECREF(encoder);
return NULL;
}
// We already checked that tags is a 2-tuple list.
key = PyTuple_GET_ITEM(item, 0);
key_int = (int)PyLong_AsLong(key);
value = PyTuple_GET_ITEM(item, 1);
Py_DECREF(item);

https://docs.python.org/3/c-api/tuple.html#c.PyTuple_GetItem

PyObject *PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
...
The returned reference is borrowed from the tuple p (that is: it is only valid as long as you hold a reference to p).
...

PyObject *PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Return value: Borrowed reference.
Like PyTuple_GetItem(), but does no checking of its arguments.

However, key and value are still used afterwards.

if (PyDict_GetItemRef(types, key, &tag_type) < 0) {

if (PyLong_Check(value)) {

@radarhere radarhere added the TIFF label Jun 22, 2026
@radarhere radarhere changed the title Do not DECREF tuple until key and value are no longer used Do not DECREF tuple until tuple items are no longer used Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant