diff mbox series

[U-Boot,v2,7/8] spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()

Message ID 20180612060502.196817-8-sjg@chromium.org
State Accepted
Commit fb95283931011aef78d885f2799ad9d7367f4e48
Delegated to: Tom Rini
Headers show
Series Fix some coverity warnings | expand

Commit Message

Simon Glass June 12, 2018, 6:05 a.m. UTC
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 <sjg@chromium.org>
---

Changes in v2:
- Fre the string when device_bind() fails, and presumably doesn't need it

 drivers/mtd/spi/sandbox.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Tom Rini June 19, 2018, 6:42 p.m. UTC | #1
On Tue, Jun 12, 2018 at 12:05:01AM -0600, Simon Glass wrote:

> 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 <sjg@chromium.org>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c
index 7893efee12..f23c0e13e0 100644
--- a/drivers/mtd/spi/sandbox.c
+++ b/drivers/mtd/spi/sandbox.c
@@ -567,16 +567,17 @@  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) {
+		free(str);
 		printf("Cannot create emul device for spec '%s' (err=%d)\n",
 		       spec, ret);
 		return ret;