summaryrefslogtreecommitdiff
path: root/py/objstringio.c
diff options
context:
space:
mode:
authorGlenn Ruben Bakke <glennbakke@gmail.com>2017-10-04 21:45:04 +0200
committerGlenn Ruben Bakke <glennbakke@gmail.com>2017-10-04 21:45:04 +0200
commitbcab2ba0a80297100919366add6bada140c6ed75 (patch)
tree5133218f4fc010d5688f006dbdd1e2f346a843f7 /py/objstringio.c
parent4468731e3d039a3f72ac25aa43e936cf5ebb3f78 (diff)
parentf869d6b2e339c04469c6c9ea3fb2fabd7bbb2d8c (diff)
ports/nrf: Upmerging port with upstream master
Diffstat (limited to 'py/objstringio.c')
-rw-r--r--py/objstringio.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/py/objstringio.c b/py/objstringio.c
index 046d32580..5c50aa317 100644
--- a/py/objstringio.c
+++ b/py/objstringio.c
@@ -28,7 +28,6 @@
#include <stdio.h>
#include <string.h>
-#include "py/nlr.h"
#include "py/objstr.h"
#include "py/objstringio.h"
#include "py/runtime.h"
@@ -118,15 +117,28 @@ STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
mp_uint_t ref = 0;
switch (s->whence) {
- case 1: // SEEK_CUR
+ case MP_SEEK_CUR:
ref = o->pos;
break;
- case 2: // SEEK_END
+ case MP_SEEK_END:
ref = o->vstr->len;
break;
}
- o->pos = ref + s->offset;
- s->offset = o->pos;
+ mp_uint_t new_pos = ref + s->offset;
+
+ // For MP_SEEK_SET, offset is unsigned
+ if (s->whence != MP_SEEK_SET && s->offset < 0) {
+ if (new_pos > ref) {
+ // Negative offset from SEEK_CUR or SEEK_END went past 0.
+ // CPython sets position to 0, POSIX returns an EINVAL error
+ new_pos = 0;
+ }
+ } else if (new_pos < ref) {
+ // positive offset went beyond the limit of mp_uint_t
+ *errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined
+ return MP_STREAM_ERROR;
+ }
+ s->offset = o->pos = new_pos;
return 0;
}
case MP_STREAM_FLUSH: