Fix some coverity warnings
Subsystems (2)
cover Fix some coverity warnings
This series attempts to fix various problems reported by Coverity, in subsystems where I have some knowledge.
Simon Glass (8):
log: Fix incorect range check in log_get_cat_name()
console: Fix handling of NULL global_data
sandbox: Use memcpy() to move overlapping regions
fdtgrep: Fix logic of free() in do_fdtgrep()
fdtgrep: Separate out checking of two allocations
rsa: Fix missing memory leak on error in fdt_add_bignum()
spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
sandbox: swap_case: Increase number of base address regs
arch/sandbox/cpu/os.c | 3 ++- common/console.c | 8 ++++++-- common/log.c | 2 +- drivers/misc/swap_case.c | 2 +- drivers/mtd/spi/sandbox.c | 6 +++--- lib/rsa/rsa-sign.c | 4 +--- tools/fdtgrep.c | 15 ++++++++++----- 7 files changed, 24 insertions(+), 16 deletions(-)
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 1/8 log: Fix incorect range check in log_get_cat_name()
This allows access to an element after the end of the array. Fix it.
---
common/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +1 -1
@@ -38,7 +38,7 @@ static const char *log_level_name[LOGL_COUNT] = {const char *log_get_cat_name (enum log_category_t cat) { -if (cat >LOGC_COUNT ) +if (cat >=LOGC_COUNT )return "invalid" ;if (cat >=LOGC_NONE )return log_cat_name[cat -LOGC_NONE ];
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 1/8 log: Fix incorect range check in log_get_cat_name()
Quoted 15 lines · Simon Glass
This allows access to an element after the end of the array. Fix it.
Reported-by Coverity (CID: 173279)Signed-off-by Simon Glass <[email protected]>---
common/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)common/log.c +0
-0
common/log.c static const char *log_level_name[LOGL_COUNT] = { · line 44
Quoted 8 lines
common/log.c +1
-1@@ -38,7 +38,7 @@ static const char *log_level_name[LOGL_COUNT] = {const char *log_get_cat_name (enum log_category_t cat) { -if (cat >LOGC_COUNT ) +if (cat >=LOGC_COUNT )return "invalid" ;if (cat >=LOGC_NONE )return log_cat_name[cat -LOGC_NONE ];
Please, consider all possible values of cat:
enums can take negative values or be an invalid uclass id. The function terminates with
return uclass_get_name((enum uclass_id)cat);
This statement will return NULL if cat does not refer to an installed uclass driver but above you decided that in case of an error you want to return "invalid". This looks inconsistent to me.
Best regards
Heinrich
PATCH 2/8 console: Fix handling of NULL global_data
In reply to Simon GlassBoth putc() and puts() can be called before global_data is set up. Some of the code paths don't handle this correctly. Add an explicit test before any member is accessed.
---
common/console.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
common/console.c +6 -2
@@ -502,8 +502,10 @@ void putc(const char c) return; }#endif +if (!gd) +return ;#ifdef CONFIG_CONSOLE_RECORD -if (gd && (gd->flags &GD_FLG_RECORD ) && gd->console_out.start) +if ((gd->flags &GD_FLG_RECORD ) && gd->console_out.start)membuff_putbyte (&gd->console_out, c);#endif #ifdef CONFIG_SILENT_CONSOLE
@@ -541,8 +543,10 @@ void puts(const char *s) return; }#endif +if (!gd) +return ;#ifdef CONFIG_CONSOLE_RECORD -if (gd && (gd->flags &GD_FLG_RECORD ) && gd->console_out.start) +if ((gd->flags &GD_FLG_RECORD ) && gd->console_out.start)membuff_put (&gd->console_out, s,strlen (s));#endif #ifdef CONFIG_SILENT_CONSOLE
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 2/8 console: Fix handling of NULL global_data
Quoted 6 lines · Simon Glass
Both
putc()andputs()can be called beforeglobal_datais set up. Some of the code paths don't handle this correctly. Add an explicit test before any member is accessed.Reported-by Coverity (CID: 169030)Signed-off-by Simon Glass <[email protected]>
PATCH 3/8 sandbox: Use memcpy() to move overlapping regions
In reply to Simon GlassThe use of strcpy() to remove characters at the start of a string is safe in U-Boot, since we know the implementation. But in os.c we are using the C library's strcpy() function, where this behaviour is not permitted.
Update the code to use memcpy() instead.
---
arch/sandbox/cpu/os.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sandbox/cpu/os.c +2 -1
@@ -587,7 +587,8 @@ int os_find_u_boot(char *fname, int maxlen)/* Look for 'u-boot' in the parent directory of spl/ */ p =strstr (fname,"/spl/" );if (p) { -strcpy (p, p +4 ); +/* Remove the "/spl" characters */ +memmove (p, p +4 ,strlen (p +4 ) +1 ); fd =os_open (fname,O_RDONLY );if (fd >=0 ) {close (fd);
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 3/8 sandbox: Use memcpy() to move overlapping regions
Quoted 19 lines · Simon Glass
The use of
strcpy()to remove characters at the start of a string is safe in U-Boot, since we know the implementation. But inos.cwe are using the C library'sstrcpy()function, where this behaviour is not permitted.Update the code to use
memcpy()instead.Reported-by Coverity (CID: 173279)Signed-off-by Simon Glass <[email protected]>---
arch/sandbox/cpu/os.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/sandbox/cpu/os.c +0
-0
arch/sandbox/cpu/os.c int os_find_u_boot(char *fname, int maxlen) · line 588
Quoted 2 lines
arch/sandbox/cpu/os.c +0
-0@@ -587,7 +587,8 @@ int os_find_u_boot(char *fname, int maxlen)/* Look for 'u-boot' in the parent directory of spl/ */ p =strstr (fname,"/spl/" );
I have built sandbox_spl_defconfig.
First observation: any parallel build fails. But that is to be handled in a seperate patch.
The logic to find u-boot here is broken. From the top directory of the git repository I executed:
$ spl/u-boot-spl
U-Boot SPL 2018.07-rc1-00063-g277daa5c0f (Jun 09 2018 - 20:40:23 +0200) Trying to boot from sandbox
(spl/u-boot not found, error -2)
SPL: failed to boot from all boot devices
### ERROR ### Please RESET the board ###
Why would you expect fname to start with /spl/?
In my case fname = 'spl/u-boot'. Why do you test for '/spl/' (only)?
Just for the record if I execute
$ ./spl/u-boot-spl everything works fine. But who would precede a directory name with './'?
Should we not concatenate the spl executable and the u-boot executable during the build process so that spl/u-boot knows where to find u-boot? That would be much safer than any assumption about relative paths.
Best regards
Heinrich
Quoted 8 lines
arch/sandbox/cpu/os.c +2
-1if (p) { -strcpy (p, p +4 ); +/* Remove the "/spl" characters */ +memmove (p, p +4 ,strlen (p +4 ) +1 ); fd =os_open (fname,O_RDONLY );if (fd >=0 ) {close (fd);
PATCH 3/8 sandbox: Use memcpy() to move overlapping regions
Hi Heinrich,
Quoted 25 lines · Heinrich Schuchardt
Quoted 19 lines · Simon Glass
The use of
strcpy()to remove characters at the start of a string is safe in U-Boot, since we know the implementation. But inos.cwe are using the C library'sstrcpy()function, where this behaviour is not permitted.Update the code to use
memcpy()instead.Reported-by Coverity (CID: 173279)Signed-off-by Simon Glass <[email protected]>---
arch/sandbox/cpu/os.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/sandbox/cpu/os.c +0
-0arch/sandbox/cpu/os.c int os_find_u_boot(char *fname, int maxlen) · line 588
Quoted 2 lines
arch/sandbox/cpu/os.c +0
-0@@ -587,7 +587,8 @@ int os_find_u_boot(char *fname, int maxlen)/* Look for 'u-boot' in the parent directory of spl/ */ p =strstr (fname,"/spl/" );I have built
sandbox_spl_defconfig.
Quoted 2 lines
First observation: any parallel build fails. But that is to be handled in a seperate patch.
I haven't seen that problem myself, but please send a patch or any details you have.
Quoted 12 lines
The logic to find u-boot here is broken. From the top directory of the git repository I executed:
$ spl/u-boot-splU-Boot SPL 2018.07-rc1-00063-g277daa5c0f (Jun 09 2018 - 20:40:23 +0200) Trying to boot from sandbox
(spl/u-boot not found, error -2)
SPL: failed to boot from all boot devices
### ERROR ### Please RESET the board ###
Quoted 7 lines
Why would you expect fname to start with /spl/?
In my case fname = 'spl/u-boot'. Why do you test for '/spl/' (only)?
Just for the record if I execute
$ ./spl/u-boot-spl everything works fine. But who would precede a directory name with './'?
It should be save enough to drop the leading / from the strstr(), if that is what you are suggesting.
Quoted 4 lines
Should we not concatenate the spl executable and the u-boot executable during the build process so that spl/u-boot knows where to find u-boot? That would be much safer than any assumption about relative paths.
We could do that, and there is support for doing this at a low level (os_jump_to_image()). I've been working on some binman enhancements to build a Chrome OS image, which would use that feature.
But executing from the filesystem seems reasonable to me, particularly for running tests.
Regards,
Simon
Quoted 14 lines
Best regards
Heinrich
Quoted 8 lines
arch/sandbox/cpu/os.c +2
-1if (p) { -strcpy (p, p +4 ); +/* Remove the "/spl" characters */ +memmove (p, p +4 ,strlen (p +4 ) +1 ); fd =os_open (fname,O_RDONLY );if (fd >=0 ) {close (fd);
PATCH 4/8 fdtgrep: Fix logic of free() in do_fdtgrep()
In reply to Simon GlassThis loop never actually exits, but the way the code is written this is not obvious. Add an explicit error check.
---
tools/fdtgrep.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/fdtgrep.c +3 -1
@@ -801,7 +801,7 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)* The first pass will count the regions, but if it is too many, * we do another pass to actually record them. */ -for (i =0 ; i <3 ; i++) { +for (i =0 ; i <2 ; i++) { region =malloc (count *sizeof (struct fdt_region ));if (!region) { fprintf(stderr, "Out of memory for %d regions\n",
@@ -820,6 +820,8 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)if (count <= max_regions)break ;free (region); +fprintf (stderr,"Internal error with fdtgrep_find_region)(\n" ); +return -1 ; }/* Optionally print a list of regions */
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 4/8 fdtgrep: Fix logic of free() in do_fdtgrep()
Quoted 16 lines · Simon Glass
This loop never actually exits, but the way the code is written this is not obvious. Add an explicit error check.
Reported-by Coverity (CID: 131280)Signed-off-by Simon Glass <[email protected]>---
tools/fdtgrep.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/fdtgrep.c +0
-0
tools/fdtgrep.c static int do_fdtgrep(struct display_info *disp, const char *filename) · line 807
Quoted 8 lines
tools/fdtgrep.c +1
-1@@ -801,7 +801,7 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)* The first pass will count the regions, but if it is too many, * we do another pass to actually record them. */ -for (i =0 ; i <3 ; i++) { +for (i =0 ; i <2 ; i++) { region =malloc (count *sizeof (struct fdt_region ));if (!region) { fprintf(stderr, "Out of memory for %d regions\n",
Can't we call fdtgrep_find_regions() with max_regions = 0 and region = NULL to do the counting and get rid of the loop? That may be a bit slower but the code will be much easier to read.
tools/fdtgrep.c static int do_fdtgrep(struct display_info *disp, const char *filename)
Quoted 1 line
tools/fdtgrep.c +0
-0@@ -820,6 +820,8 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)
Have a look at the lines in between:
if (count < 0) {
report_error("fdt_find_regions", count);
return -1;
}Here a free(region) is missing.
Best regards
Heinrich
Quoted 9 lines
tools/fdtgrep.c +2
-0if (count <= max_regions)break ;free (region); +fprintf (stderr,"Internal error with fdtgrep_find_region)(\n" ); +return -1 ; }/* Optionally print a list of regions */
PATCH 4/8 fdtgrep: Fix logic of free() in do_fdtgrep()
Hi Heinrich,
Quoted 21 lines · Heinrich Schuchardt
Quoted 20 lines · Simon Glass
This loop never actually exits, but the way the code is written this is not obvious. Add an explicit error check.
Reported-by Coverity (CID: 131280)Signed-off-by Simon Glass <[email protected]>---
tools/fdtgrep.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/fdtgrep.c +0
-1@@ -801,7 +801,7 @@ static int do_fdtgrep(struct display_info *disp, const char *filename)* The first pass will count the regions, but if it is too many, * we do another pass to actually record them. */ -for (i =0 ; i <3 ; i++) {
Quoted 8 lines
tools/fdtgrep.c static int do_fdtgrep(struct display_info *disp, const char *filename) · line 807
Quoted 4 lines
tools/fdtgrep.c +1
-0+for (i =0 ; i <2 ; i++) { region =malloc (count *sizeof (struct fdt_region ));if (!region) { fprintf(stderr, "Out of memory for %d regions\n",Can't we call
fdtgrep_find_regions()withmax_regions= 0 and region = NULL to do the counting and get rid of the loop? That may be a bit slower but the code will be much easier to read.
Yes I think that would be possible. It would require a change to fdtgrep_first_region since it assumes there is space for its region.
It seems like a good idea to me, but is beyond the scope of this coverity fix.
Regards,
Simon
PATCH 5/8 fdtgrep: Separate out checking of two allocations
In reply to Simon GlassThe current code might succeed on the first allocation and fail on the second. Separate the checks to avoid this problem.
Of course, free() will never fail and the chances that (when allocating two small areas) one will succeed and one will fail are just as remote. But this keeps coverity happy.
---
tools/fdtgrep.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/fdtgrep.c +7 -4
@@ -133,11 +133,11 @@ static int value_add(struct display_info *disp, struct value_node **headp, } str =strdup (str); +if (!str) + goto err_mem; node =malloc (sizeof (*node)); -if (!str || !node) { -fprintf (stderr,"Out of memory\n" ); -return -1 ; - } +if (!node) + goto err_mem; node->next = *headp; node->type = type; node->include = include;
@@ -145,6 +145,9 @@ static int value_add(struct display_info *disp, struct value_node **headp, *headp = node;return 0 ; +err_mem: +fprintf (stderr,"Out of memory\n" ); +return -1 ; }static bool util_is_printable_string (const void *data,int len)
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 5/8 fdtgrep: Separate out checking of two allocations
Quoted 36 lines · Simon Glass
The current code might succeed on the first allocation and fail on the second. Separate the checks to avoid this problem.
Of course,
free()will never fail and the chances that (when allocating two small areas) one will succeed and one will fail are just as remote. But this keeps coverity happy.Reported-by Coverity (CID: 131226)Signed-off-by Simon Glass <[email protected]>---
tools/fdtgrep.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)tools/fdtgrep.c +4
-4@@ -133,11 +133,11 @@ static int value_add(struct display_info *disp, struct value_node **headp, } str =strdup (str); +if (!str) + goto err_mem; node =malloc (sizeof (*node)); -if (!str || !node) { -fprintf (stderr,"Out of memory\n" ); -return -1 ; - } +if (!node) + goto err_mem; node->next = *headp; node->type = type; node->include = include;
tools/fdtgrep.c static int value_add(struct display_info *disp, struct value_node **headp, · line 146
Quoted 2 lines
tools/fdtgrep.c +0
-0@@ -145,6 +145,9 @@ static int value_add(struct display_info *disp, struct value_node **headp, *headp = node;
free(str) is missing here.
tools/fdtgrep.c static int value_add(struct display_info *disp, struct value_node **headp, · line 148
Quoted 2 lines
tools/fdtgrep.c +1
-0return 0 ; +err_mem:
free(str) is missing here.
Best regards
Heinrich
Quoted 6 lines
tools/fdtgrep.c +2
-0+fprintf (stderr,"Out of memory\n" ); +return -1 ; }static bool util_is_printable_string (const void *data,int len)
PATCH 5/8 fdtgrep: Separate out checking of two allocations
Hi Heinrich,
Quoted 33 lines · Heinrich Schuchardt
Quoted 32 lines · Simon Glass
The current code might succeed on the first allocation and fail on the second. Separate the checks to avoid this problem.
Of course,
free()will never fail and the chances that (when allocating two small areas) one will succeed and one will fail are just as remote. But this keeps coverity happy.Reported-by Coverity (CID: 131226)Signed-off-by Simon Glass <[email protected]>---
tools/fdtgrep.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)tools/fdtgrep.c +4
-4@@ -133,11 +133,11 @@ static int value_add(struct display_info *disp, struct value_node **headp, } str =strdup (str); +if (!str) + goto err_mem; node =malloc (sizeof (*node)); -if (!str || !node) { -fprintf (stderr,"Out of memory\n" ); -return -1 ; - } +if (!node) + goto err_mem;
Quoted 8 lines
tools/fdtgrep.c static int value_add(struct display_info *disp, struct value_node **headp, · line 146
Quoted 6 lines
tools/fdtgrep.c +0
-0node->next = *headp; node->type = type; node->include = include;@@ -145,6 +145,9 @@ static int value_add(struct display_info *disp, struct value_node **headp, *headp = node;free(str) is missing here.
If we free the string then the node will be invalid. That is the point of the strdup(), so that the node can have a copy of the string.
Quoted 6 lines
tools/fdtgrep.c static int value_add(struct display_info *disp, struct value_node **headp, · line 148
Quoted 4 lines
tools/fdtgrep.c +1
-0return 0 ; +err_mem:free(str) is missing here.
But the strdup() failed, so str is NULL and there is nothing to free.
Quoted 12 lines
Best regards
Heinrich
Quoted 6 lines
tools/fdtgrep.c +2
-0+fprintf (stderr,"Out of memory\n" ); +return -1 ; }static bool util_is_printable_string (const void *data,int len)
Regards,
Simon
PATCH 6/8 rsa: Fix missing memory leak on error in fdt_add_bignum()
In reply to Simon GlassThsi function can fail without freeing all its memory. Fix it.
---
lib/rsa/rsa-sign.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
lib/rsa/rsa-sign.c +1 -3
@@ -667,15 +667,13 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,* might fail several times */ ret =fdt_setprop (blob, noffset, prop_name, buf, size); -if (ret) -return -FDT_ERR_NOSPACE ;free (buf);BN_free (tmp);BN_free (big2);BN_free (big32);BN_free (big2_32); -return ret; +return ret ? -FDT_ERR_NOSPACE :0 ; }int rsa_add_verify_data (struct image_sign_info *info,void *keydest)
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 6/8 rsa: Fix missing memory leak on error in fdt_add_bignum()
Quoted 14 lines · Simon Glass
Thsi function can fail without freeing all its memory. Fix it.
Reported-by Coverity (CID: 131217)Signed-off-by Simon Glass <[email protected]>---
lib/rsa/rsa-sign.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)lib/rsa/rsa-sign.c +0
-0
lib/rsa/rsa-sign.c static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, · line 667
Quoted 1 line
lib/rsa/rsa-sign.c +0
-0@@ -667,15 +667,13 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,* might fail several times
Please, fix the memory leaks above this line too.
Best regards
Heinrich
Quoted 16 lines
lib/rsa/rsa-sign.c +1
-3*/ ret =fdt_setprop (blob, noffset, prop_name, buf, size); -if (ret) -return -FDT_ERR_NOSPACE ;free (buf);BN_free (tmp);BN_free (big2);BN_free (big32);BN_free (big2_32); -return ret; +return ret ? -FDT_ERR_NOSPACE :0 ; }int rsa_add_verify_data (struct image_sign_info *info,void *keydest)
PATCH 6/8 rsa: Fix missing memory leak on error in fdt_add_bignum()
Hi Heinrich,
Quoted 10 lines · Heinrich Schuchardt
Quoted 9 lines · Simon Glass
Thsi function can fail without freeing all its memory. Fix it.
Reported-by Coverity (CID: 131217)Signed-off-by Simon Glass <[email protected]>---
lib/rsa/rsa-sign.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 8 lines
lib/rsa/rsa-sign.c static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, · line 667
Quoted 6 lines
lib/rsa/rsa-sign.c +0
-0@@ -667,15 +667,13 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,* might fail several times Please, fix the memory leaks above this line too.
Coverity does not seem to detect these as memory leaks.
I've added a comment explaining why I don't think it makes sense to 'fix' these.
Regards,
Simon
PATCH 7/8 spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
In reply to Simon GlassMove the strdup() call so that it is only done when we know we will bind the device.
---
drivers/mtd/spi/sandbox.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/spi/sandbox.c +3 -3
@@ -567,14 +567,14 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,strncpy (name, spec,sizeof (name) -6 ); name[sizeof (name) -6 ] ='\0' ;strcat (name,"-emul" ); - str =strdup (name); -if (!str) -return -ENOMEM ; drv =lists_driver_lookup_name ("sandbox_sf_emul" );if (!drv) {puts ("Cannot find sandbox_sf_emul driver\n" );return -ENOENT ; } + str =strdup (name); +if (!str) +return -ENOMEM ; ret =device_bind (bus, drv, str,NULL , of_offset, &emul);if (ret) { printf("Cannot create emul device for spec '%s' (err=%d)\n",
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 7/8 spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
Quoted 23 lines · Simon Glass
Move the
strdup()call so that it is only done when we know we will bind the device.Reported-by Coverity (CID: 131216)Signed-off-by Simon Glass <[email protected]>---
drivers/mtd/spi/sandbox.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/mtd/spi/sandbox.c +0
-3@@ -567,14 +567,14 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,strncpy (name, spec,sizeof (name) -6 ); name[sizeof (name) -6 ] ='\0' ;strcat (name,"-emul" ); - str =strdup (name); -if (!str) -return -ENOMEM ; drv =lists_driver_lookup_name ("sandbox_sf_emul" );
drivers/mtd/spi/sandbox.c int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, · line 580
Quoted 10 lines
drivers/mtd/spi/sandbox.c +3
-0if (!drv) {puts ("Cannot find sandbox_sf_emul driver\n" );return -ENOENT ; } + str =strdup (name); +if (!str) +return -ENOMEM ; ret =device_bind (bus, drv, str,NULL , of_offset, &emul);if (ret) { printf("Cannot create emul device for spec '%s' (err=%d)\n",
Aren't you leaking str's memory here? I would have expected free(str).
Best regards
Heinrich
Quoted 1 line
PATCH 7/8 spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
Hi Heinrich,
Quoted 28 lines · Heinrich Schuchardt
Quoted 27 lines · Simon Glass
Move the
strdup()call so that it is only done when we know we will bind the device.Reported-by Coverity (CID: 131216)Signed-off-by Simon Glass <[email protected]>---
drivers/mtd/spi/sandbox.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/mtd/spi/sandbox.c +0
-3@@ -567,14 +567,14 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,strncpy (name, spec,sizeof (name) -6 ); name[sizeof (name) -6 ] ='\0' ;strcat (name,"-emul" ); - str =strdup (name); -if (!str) -return -ENOMEM ; drv =lists_driver_lookup_name ("sandbox_sf_emul" );if (!drv) {puts ("Cannot find sandbox_sf_emul driver\n" );return -ENOENT ; }
Quoted 8 lines
drivers/mtd/spi/sandbox.c int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, · line 580
Quoted 6 lines
drivers/mtd/spi/sandbox.c +3
-0+ str =strdup (name); +if (!str) +return -ENOMEM ; ret =device_bind (bus, drv, str,NULL , of_offset, &emul);if (ret) { printf("Cannot create emul device for spec '%s' (err=%d)\n",Aren't you leaking str's memory here? I would have expected free(str).
We need that for device_bind() which requires that the name be allocated for it.
Regards,
Simon
PATCH 7/8 spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
Hi Heinrich,
Quoted 40 lines · Simon Glass
Hi Heinrich,
Quoted 28 lines · Heinrich Schuchardt
Quoted 27 lines · Simon Glass
Move the
strdup()call so that it is only done when we know we will bind the device.Reported-by Coverity (CID: 131216)Signed-off-by Simon Glass <[email protected]>---
drivers/mtd/spi/sandbox.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/mtd/spi/sandbox.c +0
-3@@ -567,14 +567,14 @@ int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,strncpy (name, spec,sizeof (name) -6 ); name[sizeof (name) -6 ] ='\0' ;strcat (name,"-emul" ); - str =strdup (name); -if (!str) -return -ENOMEM ; drv =lists_driver_lookup_name ("sandbox_sf_emul" );if (!drv) {puts ("Cannot find sandbox_sf_emul driver\n" );return -ENOENT ; }Quoted 8 lines
drivers/mtd/spi/sandbox.c int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, · line 580
Quoted 6 lines
drivers/mtd/spi/sandbox.c +3
-0+ str =strdup (name); +if (!str) +return -ENOMEM ; ret =device_bind (bus, drv, str,NULL , of_offset, &emul);if (ret) { printf("Cannot create emul device for spec '%s' (err=%d)\n",Aren't you leaking str's memory here? I would have expected free(str).
Quoted 1 line
We need that for
device_bind()which requires that the name be allocated for it.
OK I see what you mean now. If device_bind() fails it should be safe to free it, since it is not supposed to leave anything around from its failure to bind the device.
Regards,
Simon
PATCH 8/8 sandbox: swap_case: Increase number of base address regs
In reply to Simon GlassAt present the code overruns the bar[] array. Fix this.
---
drivers/misc/swap_case.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/swap_case.c +1 -1
@@ -21,7 +21,7 @@*/ struct swap_case_platdata {u16 command; -u32 bar[2 ]; +u32 bar[6 ]; };#define offset_to_barnum (offset) \
Signature · 1 line
2.18.0.rc1.242.g61856ae69a-goog
PATCH 8/8 sandbox: swap_case: Increase number of base address regs
Quoted 5 lines · Simon Glass
At present the code overruns the bar[] array. Fix this.
Reported-by Coverity (CID: 131199)Signed-off-by Simon Glass <[email protected]>