From ea459dcf95d0bb04c816b71d2b85fbcd8cfc5ee4 Mon Sep 17 00:00:00 2001 From: Karel Zak 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 --- 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; } -- 2.47.2