tools: Fix handle leak in mmap_fdt function
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 tools: Fix handle leak in mmap_fdt function
From: Maks Mishin <[email protected]>
The handle 'ptr' is created at fit_common.c:91 by calling function 'mmap' and lost at fit_common.c:127:
Added call of free for ptr if ptr != MAP_FAILED.
Trigger was found by the Svace static analyzer.
--- tools/fit_common.c | 1 + 1 file changed, 1 insertion(+)
tools/fit_common.c +1 -0
@@ -123,6 +123,7 @@ err:close (fd);if (delete_on_error)unlink (fname); +free (ptr);return -1 ; }
Signature · 1 line
2.34.1
Quoted 5 lines · On Fri, Apr 18, 2025 at 11:15:23AM +0300, [email protected] wrote:
From: Maks Mishin <[email protected]>
The handle 'ptr' is created at fit_common.c:91 by calling function 'mmap' and lost at fit_common.c:127:
Added call of free forptrif ptr !=MAP_FAILED.
First, why do you free() a mmapped memory chunk? This really sounds unreasonable to me.
Second, you don't validate ptr before freeing it actually. It's possible to branch to label "err" before ptr is initialized, thus you must guard the free in case of garbage data in ptr (or just initialize ptr to NULL).
Quoted 21 lines
Trigger was found by the Svace static analyzer.
Signed-off-by Maks Mishin <[email protected]>--- tools/fit_common.c | 1 + 1 file changed, 1 insertion(+)tools/fit_common.c +1
-0@@ -123,6 +123,7 @@ err:close (fd);if (delete_on_error)unlink (fname); +free (ptr);return -1 ; }Signature · 1 line
2.34.1
Thanks,
Yao Zi