]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
authorKory Maincent (TI.com) <kory.maincent@bootlin.com>
Mon, 17 Nov 2025 15:23:07 +0000 (16:23 +0100)
committerTom Rini <trini@konsulko.com>
Sat, 22 Nov 2025 14:49:09 +0000 (08:49 -0600)
Fix two memory allocation bugs in label_boot_extension():

1. When label->fdtdir is not set, overlay_dir was used without any
   memory allocation.

2. When label->fdtdir is set, the allocation size was incorrect,
   using 'len' (just the fdtdir length) instead of 'dir_len' (which
   includes the trailing slash and null terminator).

Resolve both issues by moving the memory allocation and string
formatting outside the conditional block, resulting in clearer code
flow and correct sizing in all cases.

Closes: https://lists.denx.de/pipermail/u-boot/2025-November/602892.html
Addresses-Coverity-ID: 638558 Memory - illegal accesses (UNINIT)
Fixes: 935109cd9e97 ("boot: pxe_utils: Add extension board devicetree overlay support")
Signed-off-by: Kory Maincent (TI.com) <kory.maincent@bootlin.com>
Tested-by: Surkov Kirill <fanra3.tk@gmail.com>
boot/pxe_utils.c

index 038416203fc9b74e7a9ed5ac9f5fa00588c19a1a..836e4eb526cd1d0e44932010048b57d73699309e 100644 (file)
@@ -444,7 +444,7 @@ static void label_boot_extension(struct pxe_context *ctx,
        const struct extension *extension;
        struct fdt_header *working_fdt;
        struct alist *extension_list;
-       int ret, dir_len, len;
+       int ret, dir_len, len = 0;
        char *overlay_dir;
        const char *slash;
        ulong fdt_addr;
@@ -472,18 +472,16 @@ static void label_boot_extension(struct pxe_context *ctx,
                        slash = "/";
                else
                        slash = "";
-
-               dir_len = strlen(label->fdtdir) + strlen(slash) + 1;
-               overlay_dir = calloc(1, len);
-               if (!overlay_dir)
-                       return;
-
-               snprintf(overlay_dir, dir_len, "%s%s", label->fdtdir,
-                        slash);
        } else {
-               dir_len = 2;
-               snprintf(overlay_dir, dir_len, "/");
+               slash = "/";
        }
+       dir_len = len + strlen(slash) + 1;
+
+       overlay_dir = calloc(1, dir_len);
+       if (!overlay_dir)
+               return;
+
+       snprintf(overlay_dir, dir_len, "%s%s", label->fdtdir ?: "", slash);
 
        alist_for_each(extension, extension_list) {
                char *overlay_file;