Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,30 @@ def __index__(self):
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")

def check_strip_mutate(self, method, initial, expected):
ba = bytearray(initial)

class S:
def __buffer__(self, flags):
return memoryview(b" ")

def __release_buffer__(self, view):
ba.clear()

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.
class Evil:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func: bytearray.strip from performing segmentation violation
6 changes: 5 additions & 1 deletion Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,9 +2468,13 @@ bytearray_strip_impl_helper(PyByteArrayObject* self, PyObject* bytes, int stript
} while (right >= left && memchr(bytesptr, (unsigned char)myptr[right], byteslen));
right++;
}


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

if (bytes != Py_None)
PyBuffer_Release(&vbytes);
return PyByteArray_FromStringAndSize(myptr + left, right - left);
return res;
Comment on lines +2471 to +2477

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

}

/*[clinic input]
Expand Down
Loading