Skip to content

Commit 0cd9ab7

Browse files
committed
py/objstringio: Fix regression with handling SEEK_SET.
For SEEK_SET, offset should be treated as unsigned, to allow full-width stream sizes (e.g. 32-bit instead of 31-bit). This is now fully documented in stream.h. Also, seek symbolic constants are added.
1 parent 168350c commit 0cd9ab7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

py/objstringio.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
118118
struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
119119
mp_uint_t ref = 0;
120120
switch (s->whence) {
121-
case 1: // SEEK_CUR
121+
case MP_SEEK_CUR:
122122
ref = o->pos;
123123
break;
124-
case 2: // SEEK_END
124+
case MP_SEEK_END:
125125
ref = o->vstr->len;
126126
break;
127127
}
128128
mp_uint_t new_pos = ref + s->offset;
129-
if (s->offset < 0) {
129+
130+
// For MP_SEEK_SET, offset is unsigned
131+
if (s->whence != MP_SEEK_SET && s->offset < 0) {
130132
if (new_pos > ref) {
131133
// Negative offset from SEEK_CUR or SEEK_END went past 0.
132134
// CPython sets position to 0, POSIX returns an EINVAL error

py/stream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@
5050

5151
// Argument structure for MP_STREAM_SEEK
5252
struct mp_stream_seek_t {
53+
// If whence == MP_SEEK_SET, offset should be treated as unsigned.
54+
// This allows dealing with full-width stream sizes (16, 32, 64,
55+
// etc. bits). For other seek types, should be treated as signed.
5356
mp_off_t offset;
5457
int whence;
5558
};
5659

60+
// seek ioctl "whence" values
61+
#define MP_SEEK_SET (0)
62+
#define MP_SEEK_CUR (1)
63+
#define MP_SEEK_END (2)
64+
5765
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj);
5866
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj);
5967
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj);

0 commit comments

Comments
 (0)