Visitar URL original
avoid resizing buffer by davidism · Pull Request #3253 · pallets/werkzeug · GitHub
Skip to content

avoid resizing buffer - #3253

Merged
davidism merged 1 commit into
stablefrom
fix-limitedstream-readinto
Aug 21, 2026
Merged

davidism merged 1 commit into
stablefrom
fix-limitedstream-readinto

Conversation

@davidism

Copy link
Copy Markdown
Member

There's some weird behavior with Python bytearray slice assignment. If you assign more bytes than the size of the slice, the assigned-to buffer is resized to accommodate.

b = bytearray(10)  # LimitedStream.readinto(target)
remaining = 5  # 5 remaining bytes to read, smaller than target
temp_b = bytearray(5)  # buffer that only fits remaining bytes
out_size = 3  # internal stream.readinto(temp) only read 3 bytes instead of the remaining 5

b[:out_size] = temp_b  # assigning 5 byte buffer to 3 byte slice
# b is now 12 bytes to accommodate the 2 extra bytes

# fix:
b[:out_size] = temp_b[:out_size]  # assign same number of bytes as target slice

I doubt anyone is using readinto(bytearray), and I'm not even sure it matters if the array grows, but might as well be absolutely correct.

@davidism davidism added this to the 3.1.9 milestone Aug 21, 2026
@davidism
davidism merged commit 05ff3fe into stable Aug 21, 2026
14 checks passed
@davidism
davidism deleted the fix-limitedstream-readinto branch August 21, 2026 20:40
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant