Skip to content

Commit a5107ad

Browse files
air237jonasjelonek
authored andcommitted
mtd: fix buffer leak and fd leak in mtd_dump()
Two leaks in mtd_dump(): - The buffer allocated with malloc(erasesize) is never freed before returning, leaking erasesize bytes on every call. - The pre-existing malloc-NULL early return path also leaked the just- opened fd by returning directly instead of going through cleanup. Initialize buf to NULL, route the malloc-NULL case through the existing 'out:' label, and add free(buf) on the cleanup path so both fd and buf are released consistently on every exit. Signed-off-by: Anna Kiri <bredcorn@gmail.com> Link: openwrt/openwrt#23706 Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
1 parent 2818ac5 commit a5107ad

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

  • package/system/mtd/src

package/system/mtd/src/mtd.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ mtd_dump(const char *mtd, int part_offset, int size)
367367
{
368368
int ret = 0, offset = 0;
369369
int fd;
370-
char *buf;
370+
char *buf = NULL;
371371

372372
if (quiet < 2)
373373
fprintf(stderr, "Dumping %s ...\n", mtd);
@@ -385,8 +385,10 @@ mtd_dump(const char *mtd, int part_offset, int size)
385385
lseek(fd, part_offset, SEEK_SET);
386386

387387
buf = malloc(erasesize);
388-
if (!buf)
389-
return -1;
388+
if (!buf) {
389+
ret = -1;
390+
goto out;
391+
}
390392

391393
do {
392394
int len = (size > erasesize) ? (erasesize) : (size);
@@ -410,6 +412,7 @@ mtd_dump(const char *mtd, int part_offset, int size)
410412
} while (size > 0);
411413

412414
out:
415+
free(buf);
413416
close(fd);
414417
return ret;
415418
}

0 commit comments

Comments
 (0)