Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa73555

Browse files
committed
correct logic when pos is after the string #10467
1 parent 6bcfade commit fa73555

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

Lib/test/test_memoryio.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,11 @@ def test_readinto(self):
452452
self.assertEqual(a.tobytes(), b"1234567890d")
453453
memio.close()
454454
self.assertRaises(ValueError, memio.readinto, b)
455+
memio = self.ioclass(b"123")
456+
b = bytearray()
457+
memio.seek(42)
458+
memio.readinto(b)
459+
self.assertEqual(b, b"")
455460

456461
def test_relative_seek(self):
457462
buf = self.buftype("1234567890")

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #10467: Fix BytesIO.readinto() after seeking into a position after the
28+
end of the file.
29+
2730
- Issue #1682942: configparser supports alternative option/value delimiters.
2831

2932
- Issue #5412: configparser supports mapping protocol access.

Modules/_io/bytesio.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,20 @@ static PyObject *
430430
bytesio_readinto(bytesio *self, PyObject *buffer)
431431
{
432432
void *raw_buffer;
433-
Py_ssize_t len;
433+
Py_ssize_t len, n;
434434

435435
CHECK_CLOSED(self);
436436

437437
if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1)
438438
return NULL;
439439

440-
if (self->pos + len > self->string_size)
441-
len = self->string_size - self->pos;
440+
/* adjust invalid sizes */
441+
n = self->string_size - self->pos;
442+
if (len > n) {
443+
len = n;
444+
if (len < 0)
445+
len = 0;
446+
}
442447

443448
memcpy(raw_buffer, self->buf + self->pos, len);
444449
assert(self->pos + len < PY_SSIZE_T_MAX);

0 commit comments

Comments
 (0)