Skip to contentSkip to conversation

Fix some coverity warnings

PATCH Superseded Simon Glass 2 participants 23 messages Jun 9, 2018 – Jun 12, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Reported-by Coverity (CID: 173279)

---

 common/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +1 -1
index 3b5588ebe7..4e488eca5b 100644
@@ -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
#2 lore raw

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)

---

 common/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +0 -0
index 3b5588ebe7..4e488eca5b 100644

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

#3 lore raw

PATCH 2/8 console: Fix handling of NULL global_data

In reply to Simon Glass

Both 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.

Reported-by Coverity (CID: 169030)

---

 common/console.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
common/console.c +6 -2
index 2688af56e1..2ba33dc574 100644
@@ -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
#5 lore raw

PATCH 3/8 sandbox: Use memcpy() to move overlapping regions

In reply to 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 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.

Reported-by Coverity (CID: 173279)

---

 arch/sandbox/cpu/os.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sandbox/cpu/os.c +2 -1
index 5839932b00..496a8f9bd8 100644
@@ -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
#6 lore raw

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 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.

Reported-by Coverity (CID: 173279)

---

 arch/sandbox/cpu/os.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sandbox/cpu/os.c +0 -0
index 5839932b00..496a8f9bd8 100644

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 -1
 	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);
#7 lore raw

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 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.

Reported-by Coverity (CID: 173279)

---

 arch/sandbox/cpu/os.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sandbox/cpu/os.c +0 -0
index 5839932b00..496a8f9bd8 100644

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.

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-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 ###

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 -1
      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);
#8 lore raw

PATCH 4/8 fdtgrep: Fix logic of free() in do_fdtgrep()

In reply to 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)

---

 tools/fdtgrep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/fdtgrep.c +3 -1
index f2b8b71ed7..c4563e2289 100644
@@ -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
#9 lore raw

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)

---

 tools/fdtgrep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/fdtgrep.c +0 -0
index f2b8b71ed7..c4563e2289 100644

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 -0
 		if (count <= max_regions)
 			break;
 		free(region);
+		fprintf(stderr, "Internal error with fdtgrep_find_region)(\n");
+		return -1;
 	}
 
 	/* Optionally print a list of regions */
#10 lore raw

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)

---

 tools/fdtgrep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/fdtgrep.c +0 -1
index f2b8b71ed7..c4563e2289 100644
@@ -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() 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.

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

#11 lore raw

PATCH 5/8 fdtgrep: Separate out checking of two allocations

In reply to 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)

---

 tools/fdtgrep.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/fdtgrep.c +7 -4
index c4563e2289..5593b42203 100644
@@ -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
#12 lore raw

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)

---

 tools/fdtgrep.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/fdtgrep.c +4 -4
index c4563e2289..5593b42203 100644
@@ -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 -0
 	return 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)
#13 lore raw

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)

---

 tools/fdtgrep.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/fdtgrep.c +4 -4
index c4563e2289..5593b42203 100644
@@ -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 -0
      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;
 

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 -0
      return 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

#14 lore raw

PATCH 6/8 rsa: Fix missing memory leak on error in fdt_add_bignum()

In reply to Simon Glass

Thsi function can fail without freeing all its memory. Fix it.

Reported-by Coverity (CID: 131217)

---

 lib/rsa/rsa-sign.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
lib/rsa/rsa-sign.c +1 -3
index d2788bf79a..2a09d2b19e 100644
@@ -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
#15 lore raw

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)

---

 lib/rsa/rsa-sign.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
lib/rsa/rsa-sign.c +0 -0
index d2788bf79a..2a09d2b19e 100644

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)
#16 lore raw

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)

---

 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
index d2788bf79a..2a09d2b19e 100644
@@ -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

#17 lore raw

PATCH 7/8 spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()

In reply to 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)

---

 drivers/mtd/spi/sandbox.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/spi/sandbox.c +3 -3
index 7893efee12..ae29c034b9 100644
@@ -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
#18 lore raw

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)

---

 drivers/mtd/spi/sandbox.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/spi/sandbox.c +0 -3
index 7893efee12..ae29c034b9 100644
@@ -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 -0
 	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",

Aren't you leaking str's memory here? I would have expected free(str).

Best regards

Heinrich

Quoted 1 line
#19 lore raw

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)

---

 drivers/mtd/spi/sandbox.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/spi/sandbox.c +0 -3
index 7893efee12..ae29c034b9 100644
@@ -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

#20 lore raw

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)

---

 drivers/mtd/spi/sandbox.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/spi/sandbox.c +0 -3
index 7893efee12..ae29c034b9 100644
@@ -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

#21 lore raw

PATCH 8/8 sandbox: swap_case: Increase number of base address regs

In reply to Simon Glass

At present the code overruns the bar[] array. Fix this.

Reported-by Coverity (CID: 131199)

---

 drivers/misc/swap_case.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/misc/swap_case.c +1 -1
index 56cd0700fa..b777404c09 100644
@@ -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