tools: image-host: Fix potential memory leaks
PATCH 1/1 tools: image-host: Fix potential memory leaks
For a variable info.name memory is allocated when calling the fit_image_setup_sig function and is lost when the error code is returned.
The situation is similar for the region_prop variable,
the memory for which is allocated by calling the function fit_config_get_regions.
--- tools/image-host.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
tools/image-host.c +16 -6
@@ -237,10 +237,13 @@ static int fit_image_process_sig(const char *keydir, const char *keyfile,uint value_len;int ret; -if (fit_image_setup_sig (&info, keydir, keyfile, fit, image_name, + ret =fit_image_setup_sig (&info, keydir, keyfile, fit, image_name, noffset, require_keys ?"image" :NULL , - engine_id, algo_name)) + engine_id, algo_name); +if (ret) { +free (info.name);return -1 ; + } node_name =fit_get_name (fit, noffset,NULL ); region.data = data;
@@ -1058,15 +1061,22 @@ static int fit_config_process_sig(const char *keydir, const char *keyfile,int ret; node_name =fit_get_name (fit, noffset,NULL ); -if (fit_config_get_regions (fit, conf_noffset, noffset, ®ion, + ret =fit_config_get_regions (fit, conf_noffset, noffset, ®ion, ®ion_count, ®ion_prop, - ®ion_proplen)) + ®ion_proplen); +if (ret) { +free (region_prop);return -1 ; + } -if (fit_image_setup_sig (&info, keydir, keyfile, fit, conf_name, noffset, + ret =fit_image_setup_sig (&info, keydir, keyfile, fit, conf_name, noffset, require_keys ?"conf" :NULL , engine_id, - algo_name)) + algo_name); +if (ret) { +free (region_prop); +free (info.name);return -1 ; + } ret = info.crypto->sign (&info, region, region_count, &value, &value_len);
Signature · 1 line
2.34.1
Quoted 10 lines · Maks Mishin
For a variable
info.namememory is allocated when calling thefit_image_setup_sigfunction and is lost when the error code is returned.The situation is similar for the
region_propvariable,
the memory for which is allocated by calling the functionfit_config_get_regions.Signed-off-by Maks Mishin <[email protected]>--- tools/image-host.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
This introduces a failure to build:/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_image_process_sig’:/home/trini/work/u-boot/u-boot/tools/image-host.c:244:26: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
244 | free(info.name);
| ~~~~^~~~~
In file included from /home/trini/work/u-boot/u-boot/include/compiler.h:24,
from <command-line>:
/usr/include/stdlib.h:687:25: note: expected ‘void *’ but argument is of type ‘const char *’
687 | extern void free (void *__ptr) __THROW;
| ~~~~~~^~~~~
/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_config_process_sig’:
/home/trini/work/u-boot/u-boot/tools/image-host.c:1087:26: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
1087 | free(info.name);
| ~~~~^~~~~
/usr/include/stdlib.h:687:25: note: expected ‘void *’ but argument is of type ‘const char *’
687 | extern void free (void *__ptr) __THROW;
| ~~~~~~^~~~~In function ‘fit_config_process_sig’,
inlined from ‘fit_config_add_verification_data’ at /home/trini/work/u-boot/u-boot/tools/image-host.c:1156:10, inlined from ‘fit_add_verification_data’ at /home/trini/work/u-boot/u-boot/tools/image-host.c:1377:9:/home/trini/work/u-boot/u-boot/tools/image-host.c:1078:17: warning: ‘region_prop’ may be used uninitialized [-Wmaybe-uninitialized]
1078 | free(region_prop);
| ^~~~~~~~~~~~~~~~~
/home/trini/work/u-boot/u-boot/tools/image-host.c: In function ‘fit_add_verification_data’:
/home/trini/work/u-boot/u-boot/tools/image-host.c:1066:15: note: ‘region_prop’ was declared here
1066 | char *region_prop;
| ^~~~~~~~~~~Signature · 1 line
Tom