Skip to content

Commit 5110979

Browse files
Fixed segmentation fault error in bytearray.strip()
1 parent d7684fb commit 5110979

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎Lib/test/test_bytes.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,6 +2414,18 @@ def __index__(self):
24142414
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
24152415
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")
24162416

2417+
def test_strip_buffer_release_mutates_self(self):
2418+
ba = bytearray(b"x" * (1 << 22))
2419+
2420+
class S:
2421+
def __buffer__(self, flags):
2422+
return memoryview(b" ")
2423+
2424+
def __release_buffer__(self, view):
2425+
ba.clear()
2426+
2427+
result = ba.strip(S())
2428+
24172429
def test_search_methods_reentrancy_raises_buffererror(self):
24182430
# gh-142560: Raise BufferError if buffer mutates during search arg conversion.
24192431
class Evil:

‎Objects/bytearrayobject.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2468,9 +2468,11 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript
24682468
} while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen));
24692469
right++;
24702470
}
2471+
char temp_ptr[mysize+1];
2472+
strncpy(temp_ptr,myptr, mysize);
24712473
if (bytes != Py_None)
24722474
PyBuffer_Release(&vbytes);
2473-
return PyByteArray_FromStringAndSize(myptr + left, right - left);
2475+
return PyByteArray_FromStringAndSize(temp_ptr + left, right - left);
24742476
}
24752477

24762478
/*[clinic input]

0 commit comments

Comments
 (0)