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

gh-143375: Fix a crash in `BufferedWriter.seek` when passing an object with a specially crafted `__index__` by tomasr8 · Pull Request #143577 · python/cpython · GitHub

Repository navigation

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

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

Conversation

tomasr8 commented Jan 8, 2026 •
edited by bedevere-app Bot
Loading

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.

@@ -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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?

Copy link
Copy Markdown

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

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


Back | FazBrowse Home | New Git URL