Blame 0033-lib-path-make-ul_path_read_buffer-more-robust-coveri.patch

673c78
From f6fffc1a89e57b7d5dd4adf1ee6b2146e58ec411 Mon Sep 17 00:00:00 2001
673c78
From: Karel Zak <kzak@redhat.com>
673c78
Date: Thu, 17 Mar 2022 12:18:03 +0100
673c78
Subject: lib/path: make ul_path_read_buffer() more robust [coverity scan]
673c78
673c78
Make sure we never call buf[rc - 1] for rc=0.
673c78
673c78
Upstream: http://github.com/util-linux/util-linux/commit/ea459dcf95d0bb04c816b71d2b85fbcd8cfc5ee4
673c78
Addresses: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=2109459
673c78
Signed-off-by: Karel Zak <kzak@redhat.com>
673c78
---
673c78
 lib/path.c | 17 ++++++++++-------
673c78
 1 file changed, 10 insertions(+), 7 deletions(-)
673c78
673c78
diff --git a/lib/path.c b/lib/path.c
673c78
index 21f9bd1c4..ab034e110 100644
673c78
--- a/lib/path.c
673c78
+++ b/lib/path.c
673c78
@@ -666,14 +666,17 @@ int ul_path_readf_string(struct path_cxt *pc, char **str, const char *path, ...)
673c78
 int ul_path_read_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path)
673c78
 {
673c78
 	int rc = ul_path_read(pc, buf, bufsz - 1, path);
673c78
-	if (rc < 0)
673c78
-		return rc;
673c78
 
673c78
-	/* Remove tailing newline (usual in sysfs) */
673c78
-	if (rc > 0 && *(buf + rc - 1) == '\n')
673c78
-		buf[--rc] = '\0';
673c78
-	else
673c78
-		buf[rc - 1] = '\0';
673c78
+	if (rc == 0)
673c78
+		buf[0] = '\0';
673c78
+
673c78
+	else if (rc > 0) {
673c78
+		/* Remove tailing newline (usual in sysfs) */
673c78
+		if (*(buf + rc - 1) == '\n')
673c78
+			buf[--rc] = '\0';
673c78
+		else
673c78
+			buf[rc - 1] = '\0';
673c78
+	}
673c78
 
673c78
 	return rc;
673c78
 }
673c78
-- 
673c78
2.36.1
673c78