GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-141311: Avoid assertion in BytesIO readinto by cmaloney · Pull Request #141333 · python/cpython

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 10, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Account for when self->pos is equal to PY_SSIZE_T_MAX. The length of read was correctly set to zero but the asserts assumed self->pos couldn't reach PY_SSIZE_T_MAX. Return early to avoid edge cases.

  • Issue: Assertion failure in Modules/_io/bytesio.c _io_BytesIO_readinto_impl: Assertion 'self->pos + len < PY_SSIZE_T_MAX' failed. #141311

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@cmaloney
pythongh-141311: Avoid assertion in BytesIO readinto
db81df6
Account for when self->pos is equal to PY_SSIZE_T_MAX. The length of
read was correctly set to zero but the asserts assumed
self->pos couldn't reach PY_SSIZE_T_MAX. Return early to avoid edge
cases.
@bedevere-app bedevere-app Bot added the awaiting review label Nov 10, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Nov 10, 2025
Assertion failure in Modules/_io/bytesio.c _io_BytesIO_readinto_impl: Assertion 'self->pos + len < PY_SSIZE_T_MAX' failed. #141311
Closed
@cmaloney
Tweak comment for correctness.
34ed126
serhiy-storchaka
serhiy-storchaka reviewed Nov 10, 2025
View reviewed changes
Comment thread Modules/_io/bytesio.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_io/test_memoryio.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2025-11-09-18-55-13.gh-issue-141311.qZ3swc.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

cmaloney added 3 commits November 10, 2025 09:55
@cmaloney
Simplify by using sys.maxsize
2c84efe
@cmaloney
Reference io.BytesIO.readinto directly
d119904
@cmaloney
Resolve read_bytes_lock_held and audit other cases
d78cc84
For `read_bytes_lock_held` the `size` parameter is always set to 0 by calling
code currently if self->pos > self->string_size. I added a range check for
self->pos as an extra safety as codepaths chagne but can remove if it's too much.

`write_bytes_lock_held`: `endpos` is a `size_t` so can hold
`2 * PY_SSIZE_T_MAX`. Value is bounds checked in `resize_buffer_lock_held`.

A number of cases use `scan_eol_lock_held` to move forward. That has code which
checks `self->pos >= self->string_size` and returns `0`. Callsites all seem to
handle that correctly.
@cmaloney

cmaloney commented Nov 11, 2025

Copy link
Copy Markdown
Contributor Author

I audited the codepaths which did self->pos + buffer.

For read_bytes_lock_held the size parameter is always set to 0 by calling code currently if self->pos > self->string_size. I added a range check for self->pos as an extra safety as codepaths change but can remove if it's too much.

write_bytes_lock_held: endpos is a size_t so can hold 2 * PY_SSIZE_T_MAX. Value is bounds checked in resize_buffer_lock_held.

A number of cases use scan_eol_lock_held to move forward. That has code which checks self->pos >= self->string_size and returns 0. Callers seem to handle that correctly (early exit or call read_bytes_lock_held)..

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cmaloney
Update blurb for additional change
4c12be7
serhiy-storchaka
serhiy-storchaka reviewed Nov 11, 2025
View reviewed changes
Comment thread Modules/_io/bytesio.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_io/bytesio.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_io/bytesio.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

cmaloney added 2 commits November 11, 2025 12:51
@cmaloney
Update for review comments
e06d274
@cmaloney
tweak blurb
6cc0ab3
serhiy-storchaka
serhiy-storchaka reviewed Nov 11, 2025
View reviewed changes
Comment thread Modules/_io/bytesio.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@cmaloney
Improve precision of comments around the undefined behavior and safeties
d205d4a
serhiy-storchaka
serhiy-storchaka approved these changes Nov 11, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Nov 11, 2025
@serhiy-storchaka
serhiy-storchaka merged commit 7d54374 into python:main Nov 12, 2025
50 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 12, 2025
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Nov 12, 2025
@miss-islington-app

miss-islington-app Bot commented Nov 12, 2025

Copy link
Copy Markdown

Thanks @cmaloney for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Nov 12, 2025

Copy link
Copy Markdown

Thanks @cmaloney for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Nov 12, 2025

Copy link
Copy Markdown

Sorry, @cmaloney and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7d54374f9c7d91e0ef90c4ad84baf10073cf1d8a 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned serhiy-storchaka Nov 12, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 12, 2025
@cmaloney @miss-islington
pythongh-141311: Avoid assertion in BytesIO.readinto() (pythonGH-141333)
7151cfb
Fix error in assertion which causes failure if pos is equal to PY_SSIZE_T_MAX.
Fix undefined behavior in read() and readinto() if pos is larger that the size
of the underlying buffer.
(cherry picked from commit 7d54374)

Co-authored-by: Cody Maloney <[email protected]>
@bedevere-app

bedevere-app Bot commented Nov 12, 2025

Copy link
Copy Markdown

GH-141457 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 12, 2025
@serhiy-storchaka

serhiy-storchaka commented Nov 12, 2025

Copy link
Copy Markdown
Member

@cmaloney, could you please create a backport to 3.13 manually?

cmaloney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka pushed a commit that referenced this pull request Nov 12, 2025
@miss-islington @cmaloney
[3.14] gh-141311: Avoid assertion in BytesIO.readinto() (GH-141333) (G…
75b5157
…H-141457)

Fix error in assertion which causes failure if pos is equal to PY_SSIZE_T_MAX.
Fix undefined behavior in read() and readinto() if pos is larger that the size
of the underlying buffer.
(cherry picked from commit 7d54374)

Co-authored-by: Cody Maloney <[email protected]>
@cmaloney
cmaloney deleted the bytesio_seek_far branch November 12, 2025 19:57
@bedevere-app

bedevere-app Bot commented Nov 12, 2025

Copy link
Copy Markdown

GH-141478 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Nov 12, 2025
serhiy-storchaka pushed a commit that referenced this pull request Nov 12, 2025
@cmaloney
[3.13] gh-141311: Avoid assertion in BytesIO.readinto() (GH-141333) (G…
7c62bd5
…H-141478)

Fix error in assertion which causes failure if pos is equal to PY_SSIZE_T_MAX.
Fix undefined behavior in read() and readinto() if pos is larger that the size
of the underlying buffer.
(cherry picked from commit 7d54374)
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@cmaloney @StanFromIreland
pythongh-141311: Avoid assertion in BytesIO.readinto() (pythonGH-141333)
7592d29
Fix error in assertion which causes failure if pos is equal to PY_SSIZE_T_MAX.
Fix undefined behavior in read() and readinto() if pos is larger that the size
of the underlying buffer.
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
@matthiasgoergens matthiasgoergens mentioned this pull request Aug 5, 2026
gh-148286: Fix undefined behaviour in io.StringIO.read() after an overseek #154913
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@cmaloney @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.