From: Karel Zak Date: Thu, 17 Mar 2022 11:18:03 +0000 (+0100) Subject: lib/path: make ul_path_read_buffer() more robust [coverity scan] X-Git-Tag: v2.38~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ea459dcf95d0bb04c816b71d2b85fbcd8cfc5ee4;p=thirdparty%2Futil-linux.git 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 --- 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; }