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

gh-145376: Fix reference leaks in _collectionsmodule.c by eendebakpt · Pull Request #145421 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type 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
9 changes: 6 additions & 3 deletions Modules/_collectionsmodule.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 @@ -342,8 +342,10 @@ deque_append_lock_held(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
{
if (deque->rightindex == BLOCKLEN - 1) {
block *b = newblock(deque);
if (b == NULL)
if (b == NULL) {
Py_DECREF(item);
return -1;
}
b->leftlink = deque->rightblock;
CHECK_END(deque->rightblock->rightlink);
deque->rightblock->rightlink = b;
Expand Down Expand Up @@ -389,8 +391,10 @@ deque_appendleft_lock_held(dequeobject *deque, PyObject *item,
{
if (deque->leftindex == 0) {
block *b = newblock(deque);
if (b == NULL)
if (b == NULL) {
Py_DECREF(item);
return -1;
}
b->rightlink = deque->leftblock;
CHECK_END(deque->leftblock->leftlink);
deque->leftblock->leftlink = b;
Expand Down Expand Up @@ -564,7 +568,6 @@ deque_extendleft_impl(dequeobject *deque, PyObject *iterable)
iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
if (deque_appendleft_lock_held(deque, item, maxlen) == -1) {
Py_DECREF(item);

Copy link
Copy Markdown
Contributor

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

iternext returns a new reference. Should we leave this line unchanged?

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 agree, this is fishy. Does deque_append[left]_lock_held steal item, or not?

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

With this change, deque_appendleft_lock_held() always steal its item argument. So removing Py_DECREF(item) here is correct.

Copy link
Copy Markdown
Contributor 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

The deque_appendleft_lock_held indeed steals the second argument reference. I wrote this in the OP, but did not add a comment in the code.
If you agree, I will add a comment to the start of deque_append_lock_held/deque_appendleft_lock_held.

Copy link
Copy Markdown
Contributor

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 see now, thanks!
Maybe it is worth to add a comment, that deque_appendleft_lock_held steals reference to the second argument?

Py_DECREF(it);
return NULL;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL