dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
Blob Blame History Raw
From d9fe56d8da9015694fcba5f3dd850becff677ab5 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 20 Sep 2019 13:00:19 +0200
Subject: [PATCH] libmount: use fmemopen() in more robust way [coverity scan]

Upstream: http://github.com/karelzak/util-linux/commit/026f7d302066a4e6f5a69dc9818ec3180939f4a3
Upstream: http://github.com/karelzak/util-linux/commit/bc747dfccf511419312ec872cefa90e25d83136a
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1751447
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 libmount/src/utils.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libmount/src/utils.c b/libmount/src/utils.c
index f7d85d124..04e79f53f 100644
--- a/libmount/src/utils.c
+++ b/libmount/src/utils.c
@@ -1219,20 +1219,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
-- 
2.21.0