]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: fix memory leak in read_pte() error path master
authorKarel Zak <kzak@redhat.com>
Wed, 17 Jun 2026 09:18:18 +0000 (11:18 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 17 Jun 2026 09:18:18 +0000 (11:18 +0200)
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 <kzak@redhat.com>
libfdisk/src/dos.c

index 951962d2c6540f9b8dffaa51ad09bb557b96793d..1731c1ef123637f38f56878afa3a2947182c52a7 100644 (file)
@@ -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;