Fix dirfd() handling.

Detected by scan-build-20.
This commit is contained in:
Milan Broz
2025-02-21 14:43:49 +01:00
parent 7f0724f46c
commit 1df9a4c566
2 changed files with 13 additions and 6 deletions

View File

@@ -420,7 +420,7 @@ int lookup_by_disk_id(const char *dm_uuid)
{
struct dirent *entry;
struct stat st;
int r = 0; /* not found */
int dfd, r = 0; /* not found */
DIR *dir = opendir("/dev/disk/by-id");
if (!dir)
@@ -432,7 +432,8 @@ int lookup_by_disk_id(const char *dm_uuid)
!strncmp(entry->d_name, "..", 2))
continue;
if (fstatat(dirfd(dir), entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) {
dfd = dirfd(dir);
if (dfd < 0 || fstatat(dfd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) {
r = -EINVAL;
break;
}
@@ -457,7 +458,7 @@ int lookup_by_sysfs_uuid_field(const char *dm_uuid)
char subpath[PATH_MAX], uuid[DM_UUID_LEN];
ssize_t s;
struct stat st;
int fd, len, r = 0; /* not found */
int fd, dfd, len, r = 0; /* not found */
DIR *dir = opendir("/sys/block/");
if (!dir)
@@ -476,7 +477,10 @@ int lookup_by_sysfs_uuid_field(const char *dm_uuid)
}
/* looking for dm-X/dm/uuid file, symlinks are fine */
fd = openat(dirfd(dir), subpath, O_RDONLY | O_CLOEXEC);
dfd = dirfd(dir);
if (dfd < 0)
continue;
fd = openat(dfd, subpath, O_RDONLY | O_CLOEXEC);
if (fd < 0)
continue;