Skip to content

gh-157242: Leave bytearray unchanged if resize() fails - #157243

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:writer_resize2
Open

gh-157242: Leave bytearray unchanged if resize() fails#157243
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:writer_resize2

Conversation

@vstinner

@vstinner vstinner commented Sep 10, 2026

Copy link
Copy Markdown
Member

If bytearray.resize() or bytearray.take_bytes() fails, leave the bytearray unchanged.

If PyBytesWriter_Resize() fails, leave the writer unchanged.

Add a new internal _PyBytes_ResizeKeepOnError() function similar to _PyBytes_Resize() but leaves the bytes object unchanged on error.

If bytearray.resize() or bytearray.take_bytes() fails, leave the
bytearray unchanged.

If PyBytesWriter_Resize() fails, leave the writer unchanged.

Add a new internal _PyBytes_ResizeKeepOnError() function similar to
_PyBytes_Resize() but leaves the bytes object unchanged on error.
Comment thread Objects/bytesobject.c
}
}
else {
*pv = PyBytes_FromStringAndSize(PyBytes_AS_STRING(v), newsize);

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 included the _PyBytes_Resize() fix from PR gh-156996 in my PR. Once PR gh-156996 will be merged, this PR should be updated.

Comment thread Objects/bytearrayobject.c
if (ret == -1) {
obj->ob_bytes_object = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
size = alloc = 0;
if (_PyBytes_ResizeKeepOnError(&obj->ob_bytes_object, alloc) < 0) {

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.

@cmaloney: I'm not sure that this change is correct. Is the bytearray in a consistent state at this point? Especially, after the memmove() call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant