]> www.infradead.org Git - mtd-utils.git/commitdiff
mtd-utils: Fix "are we really at EOF" test logic in libubi read_data
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Fri, 24 Jan 2020 22:59:47 +0000 (23:59 +0100)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sun, 9 Feb 2020 21:13:18 +0000 (22:13 +0100)
The function reads file data into a buffer and then checks if we
actually are at the end-of-file by trying to read one more byte.
For whatever reason, the code uses an int instead of a char. It's
not pretty but works. But again, this is something that every
static analysis tool barks at.

Further more, the error messages are inverted. "We aren't at EOF yet"
is printed on failure and something like "read error %m" is printed
on success.

This patch fixes all of the above.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
lib/libubi.c

index aaeeb386c6518c30f5b1daedbe75cfb110e8c696..afe364870adb1b5e1f6e3b5cfc3df861e334fd64 100644 (file)
@@ -156,7 +156,8 @@ static int read_positive_int(const char *file, int *value)
  */
 static int read_data(const char *file, void *buf, int buf_len)
 {
-       int fd, rd, tmp, tmp1;
+       int fd, rd, tmp1;
+       char tmp;
 
        fd = open(file, O_RDONLY);
        if (fd == -1)
@@ -178,11 +179,11 @@ static int read_data(const char *file, void *buf, int buf_len)
 
        /* Make sure all data is read */
        tmp1 = read(fd, &tmp, 1);
-       if (tmp1 == 1) {
+       if (tmp1 < 0) {
                sys_errmsg("cannot read \"%s\"", file);
                goto out_error;
        }
-       if (tmp1) {
+       if (tmp1 > 0) {
                errmsg("file \"%s\" contains too much data (> %d bytes)",
                       file, buf_len);
                errno = EINVAL;