aboutsummaryrefslogtreecommitdiff
path: root/py/stream.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-08-20 21:57:36 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-08-20 22:02:41 +0300
commite3383e9352ca7704e650b07038207719c60b56fb (patch)
tree92ed2d9aab2ef2046ffd913b77309cf515236a63 /py/stream.c
parent0cd9ab77550eada4def492a3a25286ead71a3b24 (diff)
py/stream: seek: Consistently handle negative offset for SEEK_SET.
Per POSIX, this is EINVAL, so raises OSError(EINVAL).
Diffstat (limited to 'py/stream.c')
-rw-r--r--py/stream.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/py/stream.c b/py/stream.c
index 5d1868153..0029a59a7 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -448,11 +448,16 @@ STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
struct mp_stream_seek_t seek_s;
// TODO: Could be uint64
seek_s.offset = mp_obj_get_int(args[1]);
- seek_s.whence = 0;
+ seek_s.whence = SEEK_SET;
if (n_args == 3) {
seek_s.whence = mp_obj_get_int(args[2]);
}
+ // In POSIX, it's error to seek before end of stream, we enforce it here.
+ if (seek_s.whence == SEEK_SET && seek_s.offset < 0) {
+ mp_raise_OSError(MP_EINVAL);
+ }
+
int error;
mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
if (res == MP_STREAM_ERROR) {