Skip to content

gh-143375: Fix a crash in BufferedWriter.seek when passing an object with a specially crafted __index__ - #143577

Open
tomasr8 wants to merge 15 commits into
python:mainfrom
tomasr8:buffer-seek
Open

gh-143375: Fix a crash in BufferedWriter.seek when passing an object with a specially crafted __index__#143577
tomasr8 wants to merge 15 commits into
python:mainfrom
tomasr8:buffer-seek

Conversation

@tomasr8

@tomasr8 tomasr8 commented Jan 8, 2026

Copy link
Copy Markdown
Member

Adds a check after the call to PyNumber_AsOff_t which could have closed the file as a side-effect.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-01-08-19-56-40.gh-issue-143375.QCzA_8.rst Outdated
@@ -0,0 +1,2 @@
Fix a crash in the ``seek`` method of :class:`~io.BufferedWriter` when

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.

Is it only BufferedWriter or other buffered streams?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's also BufferedReader and BufferedRandom since they share the same implementation. I updated the news entry and added tests!

Comment thread Modules/_io/bufferedio.c Outdated
Comment on lines +1396 to +1398
// PyNumber_AsOff_t calls user code via __index__, which
// could have closed the file.
CHECK_CLOSED(self, "seek of closed file")

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.

I do not think this comment is needed. It is trivial if we do all right, and we do not what to add such comments for each second line.

Remove CHECK_CLOSED above, it is redundant. Also, I think it is better to move PyNumber_AsOff_t immediately after the whence check.

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.

Oh, and CHECK_INITIALIZED also should be after PyNumber_AsOff_t. Add a test for concurrent detach().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I do not think this comment is needed.

Removed

Remove CHECK_CLOSED above, it is redundant.

Also removed

Also, I think it is better to move PyNumber_AsOff_t immediately after the whence check.

Done

Add a test for concurrent detach().

Added!

@@ -0,0 +1,2 @@
Fix a crash in the ``seek`` method of :class:`~io.BufferedWriter` when
passing an object with a specially crafted :meth:`~object.__index__`.

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.

No, a specially crafted __index__() is not a cause, it is only a mean of reproducing the issue. The root issue is that the buffered stream can be closed (or detached?) concurrently with seek(). This can happen in other thread (or in the garbage collector), when the GIL is released in not so special custom __index__().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

True, I updated the news entry with the root cause. Let me know if it's ok!

@serhiy-storchaka

Copy link
Copy Markdown
Member

And I think TextIOWrapper has the same issue. See #143007.

@tomasr8
tomasr8 requested a review from serhiy-storchaka April 5, 2026 14:50
# Used to crash before gh-143689:
self.assertEqual(bufio.read1(1), b"h")

def test_concurrent_close(self):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure whether it's better keep the tests duplicated for each stream type or to use CommonBufferedTests (but that also runs for the Python versions, so we'd need some extra logic to skip those) or even to create CommonCBufferedTests just for the C versions?

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 17, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 24, 2026
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