]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: use fmemopen() in more robust way [coverity scan]
authorKarel Zak <kzak@redhat.com>
Fri, 20 Sep 2019 11:00:19 +0000 (13:00 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 20 Sep 2019 11:00:19 +0000 (13:00 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/utils.c

index 6b731b8af5948e36de02849123439ffb40731d09..ea154f2940d0139a6a1c769dddf59378c478a1fe 100644 (file)
@@ -1242,20 +1242,26 @@ success:
  */
 FILE *mnt_get_procfs_memstream(int fd, char **membuf)
 {
-       FILE *memf;
        size_t sz = 0;
        off_t cur;
 
+       *membuf = NULL;
+
        /* in case of error, rewind to the original position */
        cur = lseek(fd, 0, SEEK_CUR);
 
-       if (read_procfs_file(fd, membuf, &sz) == 0
-           && sz > 0
-           && (memf = fmemopen(*membuf, sz, "r")))
-               return memf;
+       if (read_procfs_file(fd, membuf, &sz) == 0 && sz > 0) {
+               FILE *memf = fmemopen(*membuf, sz, "r");
+               if (memf)
+                       return memf;    /* success */
+
+               free(membuf);
+               *membuf = NULL;
+       }
 
        /* error */
-       lseek(fd, cur, SEEK_SET);
+       if (cur != (off_t) -1)
+               lseek(fd, cur, SEEK_SET);
        return NULL;
 }
 #else