fe1ca8
From d9fe56d8da9015694fcba5f3dd850becff677ab5 Mon Sep 17 00:00:00 2001
fe1ca8
From: Karel Zak <kzak@redhat.com>
fe1ca8
Date: Fri, 20 Sep 2019 13:00:19 +0200
fe1ca8
Subject: [PATCH] libmount: use fmemopen() in more robust way [coverity scan]
fe1ca8
fe1ca8
Upstream: http://github.com/karelzak/util-linux/commit/026f7d302066a4e6f5a69dc9818ec3180939f4a3
fe1ca8
Upstream: http://github.com/karelzak/util-linux/commit/bc747dfccf511419312ec872cefa90e25d83136a
fe1ca8
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1751447
fe1ca8
Signed-off-by: Karel Zak <kzak@redhat.com>
fe1ca8
---
fe1ca8
 libmount/src/utils.c | 18 ++++++++++++------
fe1ca8
 1 file changed, 12 insertions(+), 6 deletions(-)
fe1ca8
fe1ca8
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
fe1ca8
index f7d85d124..04e79f53f 100644
fe1ca8
--- a/libmount/src/utils.c
fe1ca8
+++ b/libmount/src/utils.c
fe1ca8
@@ -1219,20 +1219,26 @@ success:
fe1ca8
  */
fe1ca8
 FILE *mnt_get_procfs_memstream(int fd, char **membuf)
fe1ca8
 {
fe1ca8
-	FILE *memf;
fe1ca8
 	size_t sz = 0;
fe1ca8
 	off_t cur;
fe1ca8
 
fe1ca8
+	*membuf = NULL;
fe1ca8
+
fe1ca8
 	/* in case of error, rewind to the original position */
fe1ca8
 	cur = lseek(fd, 0, SEEK_CUR);
fe1ca8
 
fe1ca8
-	if (read_procfs_file(fd, membuf, &sz) == 0
fe1ca8
-	    && sz > 0
fe1ca8
-	    && (memf = fmemopen(*membuf, sz, "r")))
fe1ca8
-		return memf;
fe1ca8
+	if (read_procfs_file(fd, membuf, &sz) == 0 && sz > 0) {
fe1ca8
+		FILE *memf = fmemopen(*membuf, sz, "r");
fe1ca8
+		if (memf)
fe1ca8
+			return memf;	/* success */
fe1ca8
+
fe1ca8
+		free(*membuf);
fe1ca8
+		*membuf = NULL;
fe1ca8
+	}
fe1ca8
 
fe1ca8
 	/* error */
fe1ca8
-	lseek(fd, cur, SEEK_SET);
fe1ca8
+	if (cur != (off_t) -1)
fe1ca8
+		lseek(fd, cur, SEEK_SET);
fe1ca8
 	return NULL;
fe1ca8
 }
fe1ca8
 #else
fe1ca8
-- 
fe1ca8
2.21.0
fe1ca8