From: Karel Zak Date: Wed, 17 Jun 2026 09:18:18 +0000 (+0200) Subject: libfdisk: fix memory leak in read_pte() error path X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;p=thirdparty%2Futil-linux.git libfdisk: fix memory leak in read_pte() error path Don't assign the sector buffer to the partition table entry until read_sector() succeeds. On failure, free the buffer immediately instead of leaving it in a partially initialized pte. Addresses: https://github.com/util-linux/util-linux/pull/4423 Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index 951962d2c..1731c1ef1 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -266,17 +266,17 @@ static int read_pte(struct fdisk_context *cxt, size_t pno, fdisk_sector_t offset DBG(LABEL, ul_debug("DOS: reading EBR %zu: offset=%ju, buffer=%p", pno, (uintmax_t) offset, buf)); - pe->offset = offset; - pe->sectorbuffer = buf; - pe->private_sectorbuffer = 1; - - rc = read_sector(cxt, offset, pe->sectorbuffer); + rc = read_sector(cxt, offset, buf); if (rc) { fdisk_warn(cxt, _("Failed to read extended partition table " "(offset=%ju)"), (uintmax_t) offset); + free(buf); return rc; } + pe->offset = offset; + pe->sectorbuffer = buf; + pe->private_sectorbuffer = 1; pe->changed = 0; pe->pt_entry = pe->ex_entry = NULL; return 0;