jtag: fix clang ulink memory leaks
authorSpencer Oliver <spen@spen-soft.co.uk>
Fri, 24 Aug 2012 20:33:35 +0000 (21:33 +0100)
committerFreddie Chopin <freddie.chopin@gmail.com>
Wed, 29 Aug 2012 06:25:19 +0000 (06:25 +0000)
Memory leaks discovered by clang 3.1

Change-Id: I8a784ba9726deac508424eddb27e9c8409e2773f
Signed-off-by: Spencer Oliver <spen@spen-soft.co.uk>
Reviewed-on: http://openocd.zylin.com/795
Tested-by: jenkins
Reviewed-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
Reviewed-by: Freddie Chopin <freddie.chopin@gmail.com>
src/jtag/drivers/ulink.c

index 2638d4e451607586520999b3680b7df628573a29..8ded37b3ac9265740a610a1769c8391710f13422 100644 (file)
@@ -532,6 +532,7 @@ int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
            case PAYLOAD_DIRECTION_OUT:
                    if (ulink_cmd->payload_out != NULL) {
                            LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
            case PAYLOAD_DIRECTION_OUT:
                    if (ulink_cmd->payload_out != NULL) {
                            LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
+                           free(payload);
                            return ERROR_FAIL;
                    } else {
                            ulink_cmd->payload_out = payload;
                            return ERROR_FAIL;
                    } else {
                            ulink_cmd->payload_out = payload;
@@ -541,6 +542,7 @@ int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
            case PAYLOAD_DIRECTION_IN:
                    if (ulink_cmd->payload_in_start != NULL) {
                            LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
            case PAYLOAD_DIRECTION_IN:
                    if (ulink_cmd->payload_in_start != NULL) {
                            LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
+                           free(payload);
                            return ERROR_FAIL;
                    } else {
                            ulink_cmd->payload_in_start = payload;
                            return ERROR_FAIL;
                    } else {
                            ulink_cmd->payload_in_start = payload;
@@ -896,6 +898,7 @@ int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
        if (scan_size_bits > (58 * 8)) {
                LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
                        " large payload");
        if (scan_size_bits > (58 * 8)) {
                LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
                        " large payload");
+               free(cmd);
                return ERROR_FAIL;
        }
 
                return ERROR_FAIL;
        }
 
@@ -934,8 +937,10 @@ int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
                    break;
        }
 
                    break;
        }
 
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        /* Build payload_out that is common to all scan types */
        cmd->payload_out[0] = scan_size_bytes & 0xFF;
 
        /* Build payload_out that is common to all scan types */
        cmd->payload_out[0] = scan_size_bytes & 0xFF;
@@ -993,8 +998,10 @@ int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
 
        /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = count;
        cmd->payload_out[1] = sequence;
 
        cmd->payload_out[0] = count;
        cmd->payload_out[1] = sequence;
@@ -1027,8 +1034,10 @@ int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
 
        /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = count & 0xff;
        cmd->payload_out[1] = (count >> 8) & 0xff;
 
        cmd->payload_out[0] = count & 0xff;
        cmd->payload_out[1] = (count >> 8) & 0xff;
@@ -1057,8 +1066,10 @@ int ulink_append_get_signals_cmd(struct ulink *device)
        /* CMD_GET_SIGNALS has two IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
 
        /* CMD_GET_SIGNALS has two IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
 
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        return ulink_append_queue(device, cmd);
 }
 
        return ulink_append_queue(device, cmd);
 }
@@ -1094,8 +1105,10 @@ int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
        /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = low;
        cmd->payload_out[1] = high;
 
        cmd->payload_out[0] = low;
        cmd->payload_out[1] = high;
@@ -1124,8 +1137,10 @@ int ulink_append_sleep_cmd(struct ulink *device, uint32_t us)
        /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
 
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = us & 0x00ff;
        cmd->payload_out[1] = (us >> 8) & 0x00ff;
 
        cmd->payload_out[0] = us & 0x00ff;
        cmd->payload_out[1] = (us >> 8) & 0x00ff;
@@ -1159,8 +1174,10 @@ int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in,
        /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
         * IN payload bytes */
        ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
        /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
         * IN payload bytes */
        ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        if (delay_scan_in < 0)
                cmd->payload_out[0] = 0;
 
        if (delay_scan_in < 0)
                cmd->payload_out[0] = 0;
@@ -1217,8 +1234,10 @@ int ulink_append_led_cmd(struct ulink *device, uint8_t led_state)
 
        /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = led_state;
 
 
        cmd->payload_out[0] = led_state;
 
@@ -1245,8 +1264,10 @@ int ulink_append_test_cmd(struct ulink *device)
 
        /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
 
        /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
        ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
-       if (ret != ERROR_OK)
+       if (ret != ERROR_OK) {
+               free(cmd);
                return ret;
                return ret;
+       }
 
        cmd->payload_out[0] = 0xAA;
 
 
        cmd->payload_out[0] = 0xAA;
 
@@ -2136,6 +2157,8 @@ static int ulink_init(void)
        ret = ulink_usb_open(&ulink_handle);
        if (ret != ERROR_OK) {
                LOG_ERROR("Could not open ULINK device");
        ret = ulink_usb_open(&ulink_handle);
        if (ret != ERROR_OK) {
                LOG_ERROR("Could not open ULINK device");
+               free(ulink_handle);
+               ulink_handle = NULL;
                return ret;
        }
 
                return ret;
        }
 
@@ -2157,6 +2180,8 @@ static int ulink_init(void)
                                ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY);
                if (ret != ERROR_OK) {
                        LOG_ERROR("Could not download firmware and re-numerate ULINK");
                                ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY);
                if (ret != ERROR_OK) {
                        LOG_ERROR("Could not download firmware and re-numerate ULINK");
+                       free(ulink_handle);
+                       ulink_handle = NULL;
                        return ret;
                }
        } else
                        return ret;
                }
        } else
@@ -2186,6 +2211,8 @@ static int ulink_init(void)
                        /* Bulk IN transfer failed -> unrecoverable error condition */
                        LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
                                "the USB port and re-connect, then re-run OpenOCD");
                        /* Bulk IN transfer failed -> unrecoverable error condition */
                        LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
                                "the USB port and re-connect, then re-run OpenOCD");
+                       free(ulink_handle);
+                       ulink_handle = NULL;
                        return ERROR_FAIL;
                }
 #ifdef _DEBUG_USB_COMMS_
                        return ERROR_FAIL;
                }
 #ifdef _DEBUG_USB_COMMS_

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)