boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
PATCH v3 1/1 boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
From: "Kory Maincent (TI.com)" <[email protected]>
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.
Addresses-Coverity-ID: 638558 Memory - illegal accesses (UNINIT)
--- boot/pxe_utils.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
boot/pxe_utils.c +9 -11
@@ -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;
Signature · 1 line
2.43.0
I tested this patch on PocketBeagle Rev A2 with am335x_evm_defconfig. Everything works correctly.
Quoted 10 lines · Kory Maincent
Fix two memory allocation bugs in
label_boot_extension():1. When
label->fdtdiris not set,overlay_dirwas used without any memory allocation.2. When
label->fdtdiris set, the allocation size was incorrect, using 'len' (just the fdtdir length) instead of 'dir_len' (which includes the trailing slash and null terminator).[...]
Applied to u-boot/master, thanks!
[1/1] boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
commit: 2d7eee5a55472f885474168acd3295d47a559755
Signature · 1 line
Tom