From 41a27709e9028940578a5cdae17292e432fa23fa Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 29 Nov 2021 13:25:16 +0100
Subject: [PATCH] lib/path: make path use more robust [coverity scan]

*** 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  | 5 ++++-
 lib/sysfs.c | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/path.c b/lib/path.c
index 0d357966f3..8a2b882fe4 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -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));
diff --git a/lib/sysfs.c b/lib/sysfs.c
index d8206be7ab..84af4fe3cb 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -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));
