Skip to content

gh-161688: Fixed segmentation fault error in bytearray.strip() - #161701

Open
NotorandXor256 wants to merge 6 commits into
python:mainfrom
NotorandXor256:fix/bytearry_strip
Open

NotorandXor256 wants to merge 6 commits into
python:mainfrom
NotorandXor256:fix/bytearry_strip

Conversation

@NotorandXor256

@NotorandXor256 NotorandXor256 commented Nov 9, 2026 •

Copy link
Copy Markdown

This PR fix segmentation violation in bytearray.strip()
bytearray.strip read freed memory, this PR solves it by copying the content of freed up string first and then free the string
I have add test in test_bytes.py

@bedevere-app

bedevere-app Bot commented Nov 9, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Nov 9, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@NotorandXor256

Copy link
Copy Markdown
Author

CC @StanFromIreland

@vstinner vstinner left a comment

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.

PyMem_Malloc()+strncpy() would make the method way slower.

I would prefer to use exports++ and exports-- to protect self. This way, it's not possible to modify the bytearray while it's being used.

There are more bytearray methods which should be modified with a similar fix.

@NotorandXor256

Copy link
Copy Markdown
Author

There are more bytearray methods which should be modified with a similar fix.

Should i fix those methods in this PR or open new issue and send a patch.

@vstinner

Copy link
Copy Markdown
Member

We can reuse the same issue for fixing stirp() and other methods. You may open a new PR if the fix is different.

@NotorandXor256

Copy link
Copy Markdown
Author

CC @vstinner

@vstinner vstinner left a comment

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.

Ah, I'm fine with this approach: only move PyByteArray_FromStringAndSize() call to avoid the crash.

Comment thread Lib/test/test_bytes.py Resolved
Comment thread Objects/bytearrayobject.c
Comment on lines +2471 to +2477


PyObject *res = PyByteArray_FromStringAndSize(myptr + left, right - left);

if (bytes != Py_None)
PyBuffer_Release(&vbytes);
return PyByteArray_FromStringAndSize(myptr + left, right - left);
return res;

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.

Suggested change
PyObject *res = PyByteArray_FromStringAndSize(myptr + left, right - left);
if (bytes != Py_None)
PyBuffer_Release(&vbytes);
return PyByteArray_FromStringAndSize(myptr + left, right - left);
return res;
PyObject *res = PyByteArray_FromStringAndSize(myptr + left, right - left);
if (bytes != Py_None) {
PyBuffer_Release(&vbytes);
}
return res;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

for some reseaon i couldn't commit this changes in github I will apply this changes in next commit

@vstinner

Copy link
Copy Markdown
Member

Oh wait, if S.__buffer__() calls ba.clear(), the result becomes an empty string. Hum. I'm not sure if we should allow this corner case. With my exports++ approach, an error is raised instead.

Co-authored-by: Victor Stinner <vstinner@python.org>
@NotorandXor256

NotorandXor256 commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

if S.__buffer__() calls ba.clear(), the result becomes an empty string.

bytearray.clear() funciton doc states that it delete all the members del sequence[:] but not the bytearray itself so bytearray after the opration isbytearray(b'')

With my exports++ approach, an error is raised instead.

I didn't implement exports++ or exports-- because this will forbid object re-sizing in __release_buffer__
if that is the intented behavior this is the code i would implement

    if (bytes != Py_None) {
        self->ob_exports++;
        PyBuffer_Release(&vbytes);
    }
    PyObject *res = PyByteArray_FromStringAndSize(myptr + left, right - left);
    if (bytes != Py_None) {
        self->ob_exports--;
    }
    return res;

@NotorandXor256

Copy link
Copy Markdown
Author

CC @vstinner

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.

2 participants