Skip to contentSkip to conversation

boot: pxe_utils: Fix memory allocation issues in overlay_dir handling

PATCH Superseded Kory Maincent 3 participants 4 messages Nov 13, 2025 – Nov 17, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

Fixes 935109cd9e97 ("boot: pxe_utils: Add extension board devicetree overlay support")

---

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
index 038416203fc..7549a93bdad 100644
@@ -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
Acked-by on v2 1/1
#1 lore raw

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->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).

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")

Thanks!
Quentin

#2 lore raw
In reply to Kory Maincent
Quoted 11 lines · Kory Maincent

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).

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")

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
index 038416203fc..7549a93bdad 100644
@@ -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 -7
  			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;

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;
#3 lore raw
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
index 038416203fc..7549a93bdad 100644
@@ -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