Skip to content

gh-160179: Leave bytearray empty when __init__() fails midway - #160180

Merged
gpshead merged 1 commit into
python:mainfrom
gpshead:bytearray-init-stale-bytes
Oct 22, 2026
Merged

gpshead merged 1 commit into
python:mainfrom
gpshead:bytearray-init-stale-bytes

Conversation

@gpshead

@gpshead gpshead commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Reset the bytearray to empty on the two __init__() error exits that return after resizing but before every byte is written: an out-of-range int in the list/tuple fast path, and a PyBuffer_ToContiguous() failure in the buffer path. This matches what the neighbouring non-int exit already does. The resize to 0 cannot fail here since _canresize() was checked on entry and no Python code runs in between.

Fixes #160179

ANT-2026-JQ5RCTS2

The list/tuple fast path and the buffer path resized the object before
filling it and did not undo the resize on error, leaving stale bytes.
@gpshead gpshead added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 22, 2026
@gpshead gpshead self-assigned this Oct 22, 2026
@gpshead
gpshead marked this pull request as ready for review October 22, 2026 04:22
@gpshead gpshead added the sprint label Oct 22, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Sprint Oct 22, 2026
@gpshead
gpshead merged commit 5a63d35 into python:main Oct 22, 2026
64 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@github-project-automation github-project-automation Bot moved this from Todo to Done in Sprint Oct 22, 2026
@miss-islington-app

Copy link
Copy Markdown

Sorry, @gpshead, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 5a63d3568197ac470561e95cbc744c1bc679f80d 3.14

@bedevere-app

bedevere-app Bot commented Oct 22, 2026

Copy link
Copy Markdown

GH-160202 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 22, 2026
@bedevere-app

bedevere-app Bot commented Oct 22, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 22, 2026
gpshead added a commit that referenced this pull request Oct 22, 2026
…H-160180) (#160202)

gh-160179: Leave bytearray empty when __init__() fails midway (GH-160180)

The list/tuple fast path and the buffer path resized the object before
filling it and did not undo the resize on error, leaving stale bytes.
(cherry picked from commit 5a63d35)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
gpshead added a commit that referenced this pull request Oct 22, 2026
…H-160180) (#160204)

The list/tuple fast path and the buffer path resized the object before
filling it and did not undo the resize on error, leaving stale bytes.

(cherry picked from commit 5a63d35)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bytearray.__init__() leaves stale bytes when it fails on a list or tuple

1 participant