aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2023-01-21 12:32:20 -0800
committerTheodore Ts'o <tytso@mit.edu>2023-01-27 12:38:31 -0500
commiteb5ebbc777023f7172b9ce4338dfe816eb4b1aa3 (patch)
tree85de7f25e82858f9c3bc94ba2bcf965e8f208163
parenta075bf746427810c4aab2be5d3852b10ce5dc3f1 (diff)
downloade2fsprogs-eb5ebbc777023f7172b9ce4338dfe816eb4b1aa3.tar.gz
misc/create_inode: simplify logic in scandir()
The control flow in scandir() (only used on Windows) confuses gcc into thinking that *name_list is not always set on success, which causes a -Wmaybe-uninitialized warning in __populate_fs(). As far as I can tell it's a false positive; however, avoid it by cleanly separating the success and failure cases in scandir(). Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r--misc/create_inode.c26
1 files changed, 10 insertions, 16 deletions
diff --git a/misc/create_inode.c b/misc/create_inode.c
index f4a3653bf..a3a34cd9a 100644
--- a/misc/create_inode.c
+++ b/misc/create_inode.c
@@ -765,39 +765,33 @@ static int scandir(const char *dir_name, struct dirent ***name_list,
size_t new_list_size = temp_list_size + 32;
struct dirent **new_list = (struct dirent**)realloc(
temp_list, new_list_size * sizeof(struct dirent*));
- if (new_list == NULL) {
- goto out;
- }
+ if (new_list == NULL)
+ goto out_err;
temp_list_size = new_list_size;
temp_list = new_list;
}
// add the copy of dirent to the list
temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
if (!temp_list[num_dent])
- goto out;
+ goto out_err;
memcpy(temp_list[num_dent], dent, dent->d_reclen);
num_dent++;
}
+ closedir(dir);
if (compar != NULL) {
qsort(temp_list, num_dent, sizeof(struct dirent*),
(int (*)(const void*, const void*))compar);
}
-
- // release the temp list
*name_list = temp_list;
- temp_list = NULL;
+ return num_dent;
-out:
- if (temp_list != NULL) {
- while (num_dent > 0) {
- free(temp_list[--num_dent]);
- }
- free(temp_list);
- num_dent = -1;
- }
+out_err:
closedir(dir);
- return num_dent;
+ while (num_dent > 0)
+ free(temp_list[--num_dent]);
+ free(temp_list);
+ return -1;
}
static int alphasort(const struct dirent **a, const struct dirent **b) {