]> git.sur5r.net Git - u-boot/commitdiff
spi: sandbox: Fix memory leak in sandbox_sf_bind_emul()
authorSimon Glass <sjg@chromium.org>
Tue, 12 Jun 2018 06:05:01 +0000 (00:05 -0600)
committerTom Rini <trini@konsulko.com>
Tue, 19 Jun 2018 11:31:44 +0000 (07:31 -0400)
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>
drivers/mtd/spi/sandbox.c

index 7893efee12aa519086670e95048ff4b44763738a..f23c0e13e0c87124682dee147e530dad737a56c7 100644 (file)
@@ -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;