Skip to content

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

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

gh-146250: Fix memory leak in re-initialization of SyntaxError#146251
bkap123 wants to merge 2 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__`.
@@ -0,0 +1 @@
Fixed a memory leak in :class:`SyntaxError` when re-initializing it.
Copy link
Member

@aisk aisk Mar 21, 2026

Choose a reason for hiding this comment

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

Nitpick:

Suggested change
Fixed a memory leak in :class:`SyntaxError` when re-initializing it.
Fixed a memory leak in :exc:`SyntaxError` when re-initializing it.

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.


# gh-146250: memory leak with re-initialization of SyntaxError
def test_syntax_error_memory_leak(self):
# Test crashes with ASan
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Test crashes with ASan

Comment on lines +2809 to +2810
Py_XSETREF(self->end_lineno, NULL);
Py_XSETREF(self->end_offset, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

Uae Py_CLEEAR here

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.

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