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 dirent *entry;
struct stat st; struct stat st;
int r = 0; /* not found */ int dfd, r = 0; /* not found */
DIR *dir = opendir("/dev/disk/by-id"); DIR *dir = opendir("/dev/disk/by-id");
if (!dir) if (!dir)
@@ -432,7 +432,8 @@ int lookup_by_disk_id(const char *dm_uuid)
!strncmp(entry->d_name, "..", 2)) !strncmp(entry->d_name, "..", 2))
continue; 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; r = -EINVAL;
break; break;
} }
@@ -457,7 +458,7 @@ int lookup_by_sysfs_uuid_field(const char *dm_uuid)
char subpath[PATH_MAX], uuid[DM_UUID_LEN]; char subpath[PATH_MAX], uuid[DM_UUID_LEN];
ssize_t s; ssize_t s;
struct stat st; struct stat st;
int fd, len, r = 0; /* not found */ int fd, dfd, len, r = 0; /* not found */
DIR *dir = opendir("/sys/block/"); DIR *dir = opendir("/sys/block/");
if (!dir) 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 */ /* 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) if (fd < 0)
continue; continue;

View File

@@ -51,7 +51,7 @@ static int lookup_holder_dm_name(const char *dm_uuid, dev_t devno, char **r_dm_n
char dm_subpath[PATH_MAX], data_dev_dir[PATH_MAX], uuid[DM_UUID_LEN], dm_name[PATH_MAX] = {}; char dm_subpath[PATH_MAX], data_dev_dir[PATH_MAX], uuid[DM_UUID_LEN], dm_name[PATH_MAX] = {};
ssize_t s; ssize_t s;
struct stat st; struct stat st;
int dmfd, fd, len, r = 0; /* not found */ int dmfd, fd, dfd, len, r = 0; /* not found */
DIR *dir; DIR *dir;
if (!r_dm_name) if (!r_dm_name)
@@ -84,7 +84,10 @@ static int lookup_holder_dm_name(const char *dm_uuid, dev_t devno, char **r_dm_n
} }
/* looking for dm-X/dm directory, symlinks are fine */ /* looking for dm-X/dm directory, symlinks are fine */
dmfd = openat(dirfd(dir), dm_subpath, O_DIRECTORY | O_RDONLY); dfd = dirfd(dir);
if (dfd < 0)
continue;
dmfd = openat(dfd, dm_subpath, O_DIRECTORY | O_RDONLY);
if (dmfd < 0) if (dmfd < 0)
continue; continue;