From 511097927fc23336b4d729abfc4646e3deee68f9 Mon Sep 17 00:00:00 2001 From: Notorandxor_turing <105069473+NotorandXor256@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:22:17 +0530 Subject: [PATCH 1/5] Fixed segmentation fault error in bytearray.strip() --- Lib/test/test_bytes.py | 12 ++++++++++++ Objects/bytearrayobject.c | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 67960f5709b08e..5bd31f7acf8d0e 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -2414,6 +2414,18 @@ def __index__(self): self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered") self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered") + def test_strip_buffer_release_mutates_self(self): + ba = bytearray(b"x" * (1 << 22)) + + class S: + def __buffer__(self, flags): + return memoryview(b" ") + + def __release_buffer__(self, view): + ba.clear() + + result = ba.strip(S()) + def test_search_methods_reentrancy_raises_buffererror(self): # gh-142560: Raise BufferError if buffer mutates during search arg conversion. class Evil: diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 317534c99f038d..7bd93e294972e8 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2468,9 +2468,11 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript } while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen)); right++; } + char temp_ptr[mysize+1]; + strncpy(temp_ptr,myptr, mysize); if (bytes != Py_None) PyBuffer_Release(&vbytes); - return PyByteArray_FromStringAndSize(myptr + left, right - left); + return PyByteArray_FromStringAndSize(temp_ptr + left, right - left); } /*[clinic input] From a2eb5375803b7669eb2566d3b7a541d0c6ed25bf Mon Sep 17 00:00:00 2001 From: Notorandxor_turing <105069473+NotorandXor256@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:01:20 +0530 Subject: [PATCH 2/5] using PyMem_Malloc to allocate memory instead of stack --- Objects/bytearrayobject.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 7bd93e294972e8..5e421efc912e4d 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2468,11 +2468,20 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript } while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen)); right++; } - char temp_ptr[mysize+1]; + + char *temp_ptr = PyMem_Malloc(mysize+1); + + if(temp_ptr == NULL) { + return PyErr_NoMemory(); + } + strncpy(temp_ptr,myptr, mysize); + if (bytes != Py_None) PyBuffer_Release(&vbytes); - return PyByteArray_FromStringAndSize(temp_ptr + left, right - left); + PyObject *res = PyByteArray_FromStringAndSize(temp_ptr + left, right - left); + PyMem_Free(temp_ptr); + return res; } /*[clinic input] From f9728e163b02f6b190ae6e37aeb4d04051cd5efd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:26:34 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2026-11-09-21-26-31.gh-issue-161688.NAn55a.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-21-26-31.gh-issue-161688.NAn55a.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-21-26-31.gh-issue-161688.NAn55a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-21-26-31.gh-issue-161688.NAn55a.rst new file mode 100644 index 00000000000000..fe653e0ee3b85b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-09-21-26-31.gh-issue-161688.NAn55a.rst @@ -0,0 +1 @@ +Fix :func: bytearray.strip from performing segmentation violation From fcbf282f02370ed31983ff53d95701bfcd256276 Mon Sep 17 00:00:00 2001 From: Notorandxor_turing <105069473+NotorandXor256@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:05:34 +0530 Subject: [PATCH 4/5] Removed PyMem_malloc & strncpy function call for speed boost --- Objects/bytearrayobject.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 5e421efc912e4d..0cfaadbfcde93e 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2469,18 +2469,11 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript right++; } - char *temp_ptr = PyMem_Malloc(mysize+1); - if(temp_ptr == NULL) { - return PyErr_NoMemory(); - } - - strncpy(temp_ptr,myptr, mysize); + PyObject *res = PyByteArray_FromStringAndSize(myptr + left, right - left); if (bytes != Py_None) PyBuffer_Release(&vbytes); - PyObject *res = PyByteArray_FromStringAndSize(temp_ptr + left, right - left); - PyMem_Free(temp_ptr); return res; } From 25830030162cd06bc8be10c66ff8088211a5f032 Mon Sep 17 00:00:00 2001 From: Notorandxor_turing <105069473+NotorandXor256@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:46:02 +0530 Subject: [PATCH 5/5] Update Lib/test/test_bytes.py Co-authored-by: Victor Stinner --- Lib/test/test_bytes.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 5bd31f7acf8d0e..332c6456026546 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -2414,8 +2414,8 @@ def __index__(self): self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered") self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered") - def test_strip_buffer_release_mutates_self(self): - ba = bytearray(b"x" * (1 << 22)) + def check_strip_mutate(self, method, initial, expected): + ba = bytearray(initial) class S: def __buffer__(self, flags): @@ -2424,7 +2424,19 @@ def __buffer__(self, flags): def __release_buffer__(self, view): ba.clear() - result = ba.strip(S()) + meth = getattr(ba, method) + result = meth(S()) + self.assertEqual(result, bytearray(expected)) + self.assertEqual(ba, bytearray(b'')) + + def test_strip_mutate(self): + self.check_strip_mutate('strip', b' abc ', b'abc') + + def test_lstrip_mutate(self): + self.check_strip_mutate('lstrip', b' abc ', b'abc ') + + def test_rstrip_mutate(self): + self.check_strip_mutate('rstrip', b' abc ', b' abc') def test_search_methods_reentrancy_raises_buffererror(self): # gh-142560: Raise BufferError if buffer mutates during search arg conversion.