rootdev: Fix memory leak
match_sysfs_device allocates a struct dirent that was used with
readdir_r. When the function was converted to use readdir, the malloc
wasn't removed, so this is now a memory leak. Remove the malloc
entirely, since readdir returns a pointer to its own memory.
BUG=b:225008839
TEST=FEATURES=test emerge-octopus rootdev
Change-Id: I20325e8b4baebca7ea358ccd804f761aeda2bfa1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/rootdev/+/3804964
Tested-by: Benjamin Gordon <bmgordon@chromium.org>
Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com>
Commit-Queue: Benjamin Gordon <bmgordon@chromium.org>
diff --git a/rootdev.c b/rootdev.c
index 037140c..eae6212 100644
--- a/rootdev.c
+++ b/rootdev.c
@@ -89,7 +89,7 @@
/* Walks sysfs and recurses into any directory/link that represents
* a block device to find sub-devices (partitions) for dev.
- * If dev == 0, the name fo the first device in the directory will be returned.
+ * If dev == 0, the name of the first device in the directory will be returned.
* Returns the device's name in "name" */
static int match_sysfs_device(char *name, size_t name_len,
const char *basedir, dev_t *dev, int depth) {
@@ -133,15 +133,6 @@
return found;
}
- /* Allocate a properly sized entry. */
- entry = malloc(offsetof(struct dirent, d_name) + working_path_size);
- if (!entry) {
- warn("malloc(dirent)");
- free(working_path);
- closedir(dirp);
- return found;
- }
-
while ((entry = readdir(dirp))) {
size_t candidate_len = strlen(entry->d_name);
size_t path_len = 0;