Skip to content

Commit 87fa488

Browse files
ajdlinuxsammj
authored andcommitted
lib/flash: fix resource leak in flash_setup_buffer() error paths
Some error paths in flash_setup_buffer() fail to free the flash_info struct or close the open ffs before they return. Change them to goto the cleanup code at the end. Separate the cleanup code into separate labels depending on whether we need to call ffs_close(), arch_flash_close() and talloc_free(). Signed-off-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com> Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
1 parent 81f28af commit 87fa488

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/flash/flash.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,26 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition)
8282
rc = arch_flash_init(&info->bl, NULL, true);
8383
if (rc) {
8484
pb_log("Failed to init mtd device\n");
85-
return NULL;
85+
goto out;
8686
}
8787

8888
rc = blocklevel_get_info(info->bl, &info->path, &info->size,
8989
&info->erase_granule);
9090
if (rc) {
9191
pb_log("Failed to retrieve blocklevel info\n");
92-
return NULL;
92+
goto out_flash;
9393
}
9494

9595
rc = ffs_init(0, info->size, info->bl, &info->ffs, 1);
9696
if (rc) {
9797
pb_log("%s: Failed to init ffs\n", __func__);
98-
goto out;
98+
goto out_flash;
9999
}
100100

101101
rc = partition_info(info, partition);
102102
if (rc) {
103103
pb_log("Failed to retrieve partition info\n");
104-
goto out;
104+
goto out_ffs;
105105
}
106106

107107
/* Check if there is a second flash side. If there is not, or
@@ -139,8 +139,11 @@ static struct flash_info *flash_setup_buffer(void *ctx, const char *partition)
139139
}
140140

141141
return info;
142-
out:
142+
out_ffs:
143+
ffs_close(info->ffs);
144+
out_flash:
143145
arch_flash_close(info->bl, NULL);
146+
out:
144147
talloc_free(info);
145148
return NULL;
146149
}

0 commit comments

Comments
 (0)