util-linux/backport-lib-path-make-ul_path_read_buffer-more-robust-coverity-scan.patch
z30031144 7256e99e59 Sync community patches
(cherry picked from commit 0c076c6b8e45ffb530b27a96d1e1810a97c5e9ee)
2022-11-08 14:33:06 +08:00

43 lines
1.2 KiB
Diff

From ea459dcf95d0bb04c816b71d2b85fbcd8cfc5ee4 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 17 Mar 2022 12:18:03 +0100
Subject: [PATCH] lib/path: make ul_path_read_buffer() more robust [coverity
scan]
Make sure we never call buf[rc - 1] for rc=0.
Signed-off-by: Karel Zak <kzak@redhat.com>
---
lib/path.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/lib/path.c b/lib/path.c
index 8a2b882fe4..20a3ea15d2 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -672,14 +672,17 @@ int ul_path_readf_string(struct path_cxt *pc, char **str, const char *path, ...)
int ul_path_read_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path)
{
int rc = ul_path_read(pc, buf, bufsz - 1, path);
- if (rc < 0)
- return rc;
- /* Remove tailing newline (usual in sysfs) */
- if (rc > 0 && *(buf + rc - 1) == '\n')
- buf[--rc] = '\0';
- else
- buf[rc - 1] = '\0';
+ if (rc == 0)
+ buf[0] = '\0';
+
+ else if (rc > 0) {
+ /* Remove tailing newline (usual in sysfs) */
+ if (*(buf + rc - 1) == '\n')
+ buf[--rc] = '\0';
+ else
+ buf[rc - 1] = '\0';
+ }
return rc;
}