[PATCH] tools: fit_common: fix memory leak in mmap_fdt error path

Anton Moryakov ant.v.moryakov at gmail.com
Thu May 21 21:13:58 CEST 2026


Static analysis reported that memory mapped by mmap() is not unmapped
in the error path of mmap_fdt().

If mmap() succeeds but a subsequent operation fails (fstat, ftruncate,
fdt_check_header, or fdt_open_into), the function jumps to the err:
label which closes the file descriptor but does not call munmap() on
the mapped region, causing a memory leak.

Fix: add munmap(ptr, sbuf->st_size) in the error path, guarded by
ptr != MAP_FAILED to avoid unmapping an invalid pointer.

Signed-off-by: Anton Moryakov <ant.v.moryakov at gmail.com>
---
 tools/fit_common.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/fit_common.c b/tools/fit_common.c
index d1cde16c1cb..24d985207da 100644
--- a/tools/fit_common.c
+++ b/tools/fit_common.c
@@ -123,6 +123,8 @@ err:
 		close(fd);
 	if (delete_on_error)
 		unlink(fname);
+	if (ptr != MAP_FAILED)
+		munmap(ptr, sbuf->st_size);
 
 	return -1;
 }
-- 
2.39.2



More information about the U-Boot mailing list