]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/path: make path use more robust [coverity scan]
authorKarel Zak <kzak@redhat.com>
Mon, 29 Nov 2021 12:25:16 +0000 (13:25 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 29 Nov 2021 12:25:16 +0000 (13:25 +0100)
*** CID 374365:  Null pointer dereferences  (FORWARD_NULL)
/lib/path.c: 364 in ul_path_stat()

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

index 0d357966f3024d18b7676eebdb02b94c4bdbc8ef..8a2b882fe4f49d022275548658a76efa6f2c0e85 100644 (file)
@@ -344,7 +344,7 @@ int ul_path_stat(struct path_cxt *pc, struct stat *sb, int flags, const char *pa
        int rc;
 
        if (!pc) {
-               rc = stat(path, sb);
+               rc = path ? stat(path, sb) : -EINVAL;
                DBG(CXT, ul_debug("stat '%s' [no context, rc=%d]", path, rc));
        } else {
                int dir = ul_path_get_dirfd(pc);
@@ -359,6 +359,7 @@ int ul_path_stat(struct path_cxt *pc, struct stat *sb, int flags, const char *pa
                        rc = fstat(dir, sb);    /* dir itself */
 
                if (rc && errno == ENOENT
+                   && path
                    && pc->redirect_on_enoent
                    && pc->redirect_on_enoent(pc, path, &dir) == 0)
                        rc = fstatat(dir, path, sb, 0);
@@ -372,6 +373,8 @@ int ul_path_open(struct path_cxt *pc, int flags, const char *path)
 {
        int fd;
 
+       if (!path)
+               return -EINVAL;
        if (!pc) {
                fd = open(path, flags);
                DBG(CXT, ul_debug("opening '%s' [no context]", path));
index d8206be7ab89bfb74a405424c89cca2d4c17f116..84af4fe3cb3c0672acc2bdc324ba71893ec32f3c 100644 (file)
@@ -165,7 +165,7 @@ static int sysfs_blkdev_enoent_redirect(struct path_cxt *pc, const char *path, i
 {
        struct sysfs_blkdev *blk = ul_path_get_dialect(pc);
 
-       if (blk && blk->parent && strncmp(path, "queue/", 6) == 0) {
+       if (blk && blk->parent && path && strncmp(path, "queue/", 6) == 0) {
                *dirfd = ul_path_get_dirfd(blk->parent);
                if (*dirfd >= 0) {
                        DBG(CXT, ul_debugobj(pc, "%s redirected to parent", path));