]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: make ul_path_read_buffer() more robust [coverity scan]
authorKarel Zak <kzak@redhat.com>
Thu, 17 Mar 2022 11:18:03 +0000 (12:18 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 17 Mar 2022 11:18:03 +0000 (12:18 +0100)
Make sure we never call buf[rc - 1] for rc=0.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/path.c

index 8a2b882fe4f49d022275548658a76efa6f2c0e85..20a3ea15d21b24e32a3165807034d941b3d56955 100644 (file)
@@ -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;
 }