dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0028-libmount-use-fmemopen-in-more-robust-way-coverity-sc.patch

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