diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c index 5773709866d2166fb8b8db42fa311ae4e532d5c6..7fc16204daad7551d83ecf0e83b3383230cc1e00 100644 --- a/src/stdio/open_memstream.c +++ b/src/stdio/open_memstream.c @@ -13,21 +13,12 @@ static off_t ms_seek(FILE *f, off_t off, int whence) { ssize_t base; struct cookie *c = f->cookie; - switch (whence) { - case SEEK_SET: - base = 0; - break; - case SEEK_CUR: - base = c->pos; - break; - case SEEK_END: - base = c->len; - break; - default: - fail: + if (whence>2U) { +fail: errno = EINVAL; return -1; } + base = (size_t [3]){0, c->pos, c->len}[whence]; if (off < -base || off > SSIZE_MAX-base) goto fail; return c->pos = base+off; } diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c index 41b92d2152f58a402f5d4e965ab4e9cc8be841c2..0db77416cec819ce1cc7782bf68fd31c8955eb4a 100644 --- a/src/stdio/open_wmemstream.c +++ b/src/stdio/open_wmemstream.c @@ -14,21 +14,12 @@ static off_t wms_seek(FILE *f, off_t off, int whence) { ssize_t base; struct cookie *c = f->cookie; - switch (whence) { - case SEEK_SET: - base = 0; - break; - case SEEK_CUR: - base = c->pos; - break; - case SEEK_END: - base = c->len; - break; - default: - fail: + if (whence>2U) { +fail: errno = EINVAL; return -1; } + base = (size_t [3]){0, c->pos, c->len}[whence]; if (off < -base || off > SSIZE_MAX/4-base) goto fail; memset(&c->mbs, 0, sizeof c->mbs); return c->pos = base+off;