FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-159739: Handle overlapping buffers in BytesIO.readinto (GH-… · python/cpython@f0986f3 · GitHub

Repository navigation

Commit f0986f3

Browse files
authored
[3.15] gh-159739: Handle overlapping buffers in BytesIO.readinto (GH-159777) (#159902)
(cherry picked from commit 17df836) Co-authored-by: Jelle Zijlstra <jelle@openai.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 37434b1 commit f0986f3

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ class CBytesIOTest(PyBytesIOTest):
859859
ioclass = io.BytesIO
860860
UnsupportedOperation = io.UnsupportedOperation
861861

862+
def test_readinto_overlapping_buffer(self):
863+
with self.ioclass(b'abcd') as stream:
864+
with stream.getbuffer()[1:] as target:
865+
self.assertEqual(stream.readinto(target), 3)
866+
self.assertEqual(stream.getvalue(), b'aabc')
867+
862868
def test_getstate(self):
863869
memio = self.ioclass()
864870
state = memio.__getstate__()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :class:`io.BytesIO` reads into overlapping views of the same buffer.

‎Modules/_io/bytesio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer)
670670

671671
assert(self->pos + len <= PY_SSIZE_T_MAX);
672672
assert(len >= 0);
673-
memcpy(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
673+
memmove(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
674674
self->pos += len;
675675

676676
return PyLong_FromSsize_t(len);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL