Skip to content

gh-146250: Fix memory leak in re-initialization of SyntaxError#146251

Open
bkap123 wants to merge 3 commits intopython:mainfrom
bkap123:syntax-error-leak
Open

gh-146250: Fix memory leak in re-initialization of SyntaxError#146251
bkap123 wants to merge 3 commits intopython:mainfrom
bkap123:syntax-error-leak

Conversation

@bkap123
Copy link
Contributor

@bkap123 bkap123 commented Mar 21, 2026

Added Py_XDECREF/Py_XSETREF to avoid a memory leak when calling SyntaxError.__init__.

Added `Py_XDECREF`/`Py_XSETREF` to avoid a memory leak when calling
`SyntaxError.__init__`.
self.assertGreater(len(output), 0) # At minimum, should not hang
self.assertIn(b"MemoryError", output)

# gh-146250: memory leak with re-initialization of SyntaxError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this comment in the test instead above it.

Comment on lines +2805 to +2808
Py_XDECREF(self->filename);
Py_XDECREF(self->lineno);
Py_XDECREF(self->offset);
Py_XDECREF(self->text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should instead create temp vara and use XSETREF, as we do for AttributeError. It would be much cleaner.

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@picnixz
Copy link
Member

picnixz commented Mar 21, 2026

Sorry about the Py_CLEAR suggestion but I think creating new variables would be cleaner.

@bkap123
Copy link
Contributor Author

bkap123 commented Mar 21, 2026

What's wrong with using Py_CLEAR?

@picnixz
Copy link
Member

picnixz commented Mar 21, 2026

I think it is just better to hold temp vars and use XSETREF like in AttributeError.__init__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants