[Concept,05/26] boot: pxe: Fix memory leak in parse_fdtoverlays()

Message ID 20260110202906.187370-6-sjg@u-boot.org
State New
Headers
Series boot: pxe: Add three-phase API and fix memory leaks |

Commit Message

Simon Glass Jan. 10, 2026, 8:28 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

The parse_sliteral() call allocates a string for val, but it is never
freed after parsing the overlay paths. Each path is duplicated by
strndup/strdup, so the original string can be freed.

Save the original pointer and free it on exit and error paths.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 boot/pxe_parse.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Patch

diff --git a/boot/pxe_parse.c b/boot/pxe_parse.c
index e5af05d4120..f6b7887f603 100644
--- a/boot/pxe_parse.c
+++ b/boot/pxe_parse.c
@@ -317,12 +317,13 @@  static int parse_sliteral(char **c, char **dst)
  */
 static int parse_fdtoverlays(char **c, struct alist *overlays)
 {
-	char *val;
+	char *val, *start;
 	int err;
 
 	err = parse_sliteral(c, &val);
 	if (err < 0)
 		return err;
+	start = val;
 
 	while (*val) {
 		struct pxe_fdtoverlay item;
@@ -348,10 +349,13 @@  static int parse_fdtoverlays(char **c, struct alist *overlays)
 
 		if (!item.path || !alist_add(overlays, item)) {
 			free(item.path);
+			free(start);
 			return -ENOMEM;
 		}
 	}
 
+	free(start);
+
 	return 1;
 }