mtd: check malloc() return value in mtd_check()

After malloc(erasesize) in mtd_check(), the result was never checked
for NULL. On allocation failure, the buf pointer remained NULL and
would later be used in image_check() via read(imagefd, buf + buflen,
...), causing a NULL pointer dereference.

Add a NULL check after the allocation and return early. The early
return path also closes the just-opened fd and frees the strdup'd
colon-separated device-list copy to avoid leaks.

Signed-off-by: Anna Kiri <bredcorn@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/23705
Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
This commit is contained in:
Anna Kiri
2026-06-08 11:06:32 +02:00
committed by Jonas Jelonek
parent 2f809f9cff
commit 2818ac5ccd
+5
View File
@@ -269,6 +269,11 @@ static int mtd_check(const char *mtd)
if (!buf)
buf = malloc(erasesize);
if (!buf) {
close(fd);
free(str);
return 0;
}
close(fd);
mtd = next;