All these checks are really testing is that resulting position is
within [0; f->size] interval. Convert all of the custom checks into a
signle one done after the switch statement to simplify the code.

Note this change also disables the validity check for f->size ==
FILE_SIZE_STREAM and whence == SEEK_END, but lseek(stream_fd, offset,
SEEK_END) wasn't a meaningful operation to begin with, so this
shouldn't be a problem.

Signed-off-by: Andrey Smirnov <andrew.smir...@gmail.com>
---
 fs/fs.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 2638e7458..d785c0c3a 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -418,26 +418,23 @@ loff_t lseek(int fildes, loff_t offset, int whence)
 
        switch (whence) {
        case SEEK_SET:
-               if (f->size != FILE_SIZE_STREAM && offset > f->size)
-                       goto out;
-               if (offset < 0)
-                       goto out;
-               pos = offset;
+               pos = 0;
                break;
        case SEEK_CUR:
-               if (f->size != FILE_SIZE_STREAM && offset + f->pos > f->size)
-                       goto out;
-               pos = f->pos + offset;
+               pos = f->pos;
                break;
        case SEEK_END:
-               if (offset > 0)
-                       goto out;
-               pos = f->size + offset;
+               pos = f->size;
                break;
        default:
                goto out;
        }
 
+       pos += offset;
+
+       if (f->size != FILE_SIZE_STREAM && (pos < 0 || pos > f->size))
+               goto out;
+
        if (fsdrv->lseek) {
                ret = fsdrv->lseek(&f->fsdev->dev, f, pos);
                if (ret < 0)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to