boot: pxe_utils: Fix memory allocation issues in overlay_dir handling
PATCH v2 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.
---
Change in v2:
- Extract calloc and snprintf of the if condition.
--- boot/pxe_utils.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
boot/pxe_utils.c +8 -8
@@ -473,18 +473,18 @@ static void label_boot_extension(struct pxe_context *ctx,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); + dir_len = len +strlen (slash) +1 ; }else { dir_len =2 ; -snprintf (overlay_dir, dir_len,"/" ); + slash ="/" ; } + 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;ulong size;
Signature · 1 line
2.43.0
Hi Köry,
Quoted 11 lines · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
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).
Quoted 7 lines
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.
Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetree overlay support")Signed-off-by Kory Maincent (TI.com) <[email protected]>
Thanks!
Quentin
Quoted 11 lines · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
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).
Quoted 7 lines
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.
Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetree overlay support")Signed-off-by Kory Maincent (TI.com) <[email protected]>
Please, add
Addresses-Coverity-ID: 638558 Memory - illegal accesses (UNINIT)
so that we know that the Coverity issue is resolved.
Quoted 14 lines
---
Change in v2:
- Extract calloc and snprintf of the if condition.--- boot/pxe_utils.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)boot/pxe_utils.c +0
-0@@ -473,18 +473,18 @@ static void label_boot_extension(struct pxe_context *ctx,else
boot/pxe_utils.c static void label_boot_extension(struct pxe_context *ctx, · line 476
Quoted 10 lines
boot/pxe_utils.c +1
-7slash ="" ; - 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); + dir_len = len +strlen (slash) +1 ;
This line could be moved after the else branch to simplify the code by avoiding separate assignments to dir_len.
Best regards
Heinrich
Quoted 15 lines
boot/pxe_utils.c +7
-1}else { dir_len =2 ; -snprintf (overlay_dir, dir_len,"/" ); + slash ="/" ; } + 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;ulong size;
Quoted 2 lines · Heinrich Schuchardt
Quoted 1 line · Kory Maincent
From: "Kory Maincent (TI.com)" <[email protected]>
[...]
Quoted 18 lines
Quoted 18 lines
---
Change in v2:
- Extract calloc and snprintf of the if condition.
---
boot/pxe_utils.c| 16 ++++++++--------1 file changed, 8 insertions(+), 8 deletions(-)boot/pxe_utils.c +0
-0@@ -473,18 +473,18 @@ static void label_boot_extension(struct pxe_context *ctx,else
slash = "";
-dir_len=strlen(label->fdtdir)+ strlen(slash) + 1; -overlay_dir=calloc(1, len);
Quoted 9 lines
Quoted 6 lines
- if (!overlay_dir)
- return;
-
-snprintf(overlay_dir, dir_len, "%s%s", label->fdtdir, - slash);
+dir_len= len + strlen(slash) + 1;This line could be moved after the else branch to simplify the code by avoiding separate assignments to
dir_len.
We need to init len to 0 for that to work when label->fdtdir is NULL.
Cheers,
Quentin