jtag: drivers: with pointers, use NULL instead of 0 92/7592/3
authorAntonio Borneo <borneo.antonio@gmail.com>
Sat, 8 Apr 2023 21:21:33 +0000 (23:21 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Fri, 5 May 2023 22:07:37 +0000 (22:07 +0000)
Don't compare pointers with 0, use NULL when needed.
Don't assign pointer to 0, use NULL.
Don't pass 0 ad pointer argument, pass NULL.

While there, check for return value from malloc(), replace an
assert() with a LOG_ERROR(), drop a useless cast.

Detected through 'sparse' tool.

Change-Id: Ia7cf52221b12198aba1a07ebdfaf57ce341d5699
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7592
Tested-by: jenkins
14 files changed:
src/jtag/drivers/arm-jtag-ew.c
src/jtag/drivers/at91rm9200.c
src/jtag/drivers/ep93xx.c
src/jtag/drivers/ft232r.c
src/jtag/drivers/ftdi.c
src/jtag/drivers/mpsse.c
src/jtag/drivers/opendous.c
src/jtag/drivers/parport.c
src/jtag/drivers/rlink.c
src/jtag/drivers/stlink_usb.c
src/jtag/drivers/sysfsgpio.c
src/jtag/drivers/ti_icdi_usb.c
src/jtag/drivers/usbprog.c
src/jtag/drivers/xds110.c

index 7db3c7b15a07cec71266b77943ac3e5395ecadc9..a372720849db3809a0b66dca8908dbfc8d0473f5 100644 (file)
@@ -213,7 +213,7 @@ static int armjtagew_init(void)
 
        armjtagew_handle = armjtagew_usb_open();
 
-       if (armjtagew_handle == 0) {
+       if (!armjtagew_handle) {
                LOG_ERROR(
                        "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
index 08daa004bf8ce3e16c47ff58ca26f65582529bbb..ba9ee5e34d22b96e502cfa15c2ff0d46d494c847 100644 (file)
@@ -107,7 +107,7 @@ static int at91rm9200_quit(void);
 static struct bitbang_interface at91rm9200_bitbang = {
        .read = at91rm9200_read,
        .write = at91rm9200_write,
-       .blink = 0
+       .blink = NULL,
 };
 
 static bb_value_t at91rm9200_read(void)
@@ -157,8 +157,12 @@ COMMAND_HANDLER(at91rm9200_handle_device_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        /* only if the device name wasn't overwritten by cmdline */
-       if (at91rm9200_device == 0) {
+       if (!at91rm9200_device) {
                at91rm9200_device = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
+               if (!at91rm9200_device) {
+                       LOG_ERROR("Out of memory");
+                       return ERROR_FAIL;
+               }
                strcpy(at91rm9200_device, CMD_ARGV[0]);
        }
 
index 5cb6dff24d8503974b59d5c4aa38b8bc494ca3d7..c3e841d37abeb75cc5910994c26e74e4deef8abe 100644 (file)
@@ -58,7 +58,7 @@ struct adapter_driver ep93xx_adapter_driver = {
 static struct bitbang_interface ep93xx_bitbang = {
        .read = ep93xx_read,
        .write = ep93xx_write,
-       .blink = 0,
+       .blink = NULL,
 };
 
 static bb_value_t ep93xx_read(void)
index 816b2d05e72b04a980aaaa2a1b870b0fe0df6d9d..1d73af4e5c24e7d78ccdcd78c22b280265df9133 100644 (file)
@@ -235,7 +235,7 @@ static int ft232r_speed(int divisor)
 
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_SET_BAUD_RATE, divisor, 0, 0, 0, 1000) != 0) {
+               SIO_SET_BAUD_RATE, divisor, 0, NULL, 0, 1000) != 0) {
                LOG_ERROR("cannot set baud rate");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -266,7 +266,7 @@ static int ft232r_init(void)
        /* Reset the device. */
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_RESET, 0, 0, 0, 0, 1000) != 0) {
+               SIO_RESET, 0, 0, NULL, 0, 1000) != 0) {
                LOG_ERROR("unable to reset device");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -275,7 +275,7 @@ static int ft232r_init(void)
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                SIO_SET_BITMODE, (1<<tck_gpio) | (1<<tdi_gpio) | (1<<tms_gpio) | (1<<ntrst_gpio) | (1<<nsysrst_gpio) | 0x400,
-               0, 0, 0, 1000) != 0) {
+               0, NULL, 0, 1000) != 0) {
                LOG_ERROR("cannot set sync bitbang mode");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -288,13 +288,13 @@ static int ft232r_init(void)
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                SIO_SET_BAUD_RATE, divisor,
-               0, 0, 0, 1000) != 0) {
+               0, NULL, 0, 1000) != 0) {
                LOG_ERROR("cannot set baud rate");
                return ERROR_JTAG_INIT_FAILED;
        }
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_SET_LATENCY_TIMER, latency_timer, 0, 0, 0, 1000) != 0) {
+               SIO_SET_LATENCY_TIMER, latency_timer, 0, NULL, 0, 1000) != 0) {
                LOG_ERROR("unable to set latency timer");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -315,7 +315,7 @@ static int ft232r_quit(void)
                if (jtag_libusb_control_transfer(adapter,
                        LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                        SIO_SET_BITMODE, ft232r_restore_bitmode,
-                       0, 0, 0, 1000) != 0) {
+                       0, NULL, 0, 1000) != 0) {
                        LOG_ERROR("cannot set bitmode to restore serial port");
                }
        }
index 6356a4929b11a34074f35b386c826136726b00d7..85b45130832bbb97d8847283ffb0ad43566a79b4 100644 (file)
@@ -181,7 +181,7 @@ static int ftdi_set_signal(const struct signal *s, char value)
                oe = s->invert_oe;
                break;
        default:
-               assert(0 && "invalid signal level specifier");
+               LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value, value);
                return ERROR_FAIL;
        }
 
index 9f2fdde9fb77fe50810ab5e796df5bcb97692024..fad91dde2f80019a7be4227416f6d7098342e9ea 100644 (file)
@@ -313,7 +313,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const
        int err;
 
        if (!ctx)
-               return 0;
+               return NULL;
 
        bit_copy_queue_init(&ctx->read_queue);
        ctx->read_chunk_size = 16384;
@@ -348,7 +348,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const
                                description ? description : "*",
                                serial ? serial : "*",
                                location ? location : "*");
-               ctx->usb_dev = 0;
+               ctx->usb_dev = NULL;
                goto error;
        }
 
@@ -378,7 +378,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const
        return ctx;
 error:
        mpsse_close(ctx);
-       return 0;
+       return NULL;
 }
 
 void mpsse_close(struct mpsse_ctx *ctx)
@@ -465,13 +465,13 @@ static unsigned buffer_add_read(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_
 void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
        unsigned length, uint8_t mode)
 {
-       mpsse_clock_data(ctx, out, out_offset, 0, 0, length, mode);
+       mpsse_clock_data(ctx, out, out_offset, NULL, 0, length, mode);
 }
 
 void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_offset, unsigned length,
        uint8_t mode)
 {
-       mpsse_clock_data(ctx, 0, 0, in, in_offset, length, mode);
+       mpsse_clock_data(ctx, NULL, 0, in, in_offset, length, mode);
 }
 
 void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in,
@@ -548,7 +548,7 @@ void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_of
 void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
        unsigned length, bool tdi, uint8_t mode)
 {
-       mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0, length, tdi, mode);
+       mpsse_clock_tms_cs(ctx, out, out_offset, NULL, 0, length, tdi, mode);
 }
 
 void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in,
@@ -842,7 +842,7 @@ int mpsse_flush(struct mpsse_ctx *ctx)
        if (ctx->write_count == 0)
                return retval;
 
-       struct libusb_transfer *read_transfer = 0;
+       struct libusb_transfer *read_transfer = NULL;
        struct transfer_result read_result = { .ctx = ctx, .done = true };
        if (ctx->read_count) {
                buffer_write_byte(ctx, 0x87); /* SEND_IMMEDIATE */
index c353aef407dd654fef0808cb819a68b7873a1c19..ad980bf23810725776eaf8dd111dd44d5029b8e7 100644 (file)
@@ -347,7 +347,7 @@ static int opendous_init(void)
 
        opendous_jtag_handle = opendous_usb_open();
 
-       if (opendous_jtag_handle == 0) {
+       if (!opendous_jtag_handle) {
                LOG_ERROR("Cannot find opendous Interface! Please check connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
        }
index 4073d066e6f21eedfae5773d19954b2454323023..d26a51048fec10a1a2ed832581349933955493a0 100644 (file)
@@ -411,9 +411,13 @@ COMMAND_HANDLER(parport_handle_parport_cable_command)
                return ERROR_OK;
 
        /* only if the cable name wasn't overwritten by cmdline */
-       if (parport_cable == 0) {
+       if (!parport_cable) {
                /* REVISIT first verify that it's listed in cables[] ... */
                parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char));
+               if (!parport_cable) {
+                       LOG_ERROR("Out of memory");
+                       return ERROR_FAIL;
+               }
                strcpy(parport_cable, CMD_ARGV[0]);
        }
 
index c933b3e1c4b9d653ab86bcbf16ed8646790f20ad..65f7494bd9cf956d519dae1906938a12ece66dfa 100644 (file)
@@ -632,7 +632,7 @@ static int dtc_queue_run(void)
        uint8_t dtc_mask, tdo_mask;
        uint8_t reply_buffer[USB_EP2IN_SIZE];
 
-       assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
+       assert((!!dtc_queue.rq_head) == (dtc_queue.reply_index > 0));
        assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
        assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
 
index 5b051c1ff9dff034b3e71f9a2d1a083155bdbdff..ee629542ba7f3f153f3aaa11143040a4c73370c3 100644 (file)
@@ -3694,7 +3694,7 @@ static int stlink_open(struct hl_interface_param_s *param, enum stlink_mode mode
 
        h = calloc(1, sizeof(struct stlink_usb_handle_s));
 
-       if (h == 0) {
+       if (!h) {
                LOG_DEBUG("malloc failed");
                return ERROR_FAIL;
        }
index ee254d66a9854877574f318e03fe056a77494979..a5f5fd3ac0310c6de32f954fb8322c80b9946c55 100644 (file)
@@ -571,7 +571,7 @@ static struct bitbang_interface sysfsgpio_bitbang = {
        .swdio_read = sysfsgpio_swdio_read,
        .swdio_drive = sysfsgpio_swdio_drive,
        .swd_write = sysfsgpio_swd_write,
-       .blink = 0
+       .blink = NULL,
 };
 
 /* helper func to close and cleanup files only if they were valid/ used */
index ca52559d1890d43900387e5801a2785306ea661d..3e36218738ec8d574961c12fecea453d97af2035 100644 (file)
@@ -369,7 +369,7 @@ static int icdi_usb_query(void *handle)
        if (h->max_packet != ICDI_PACKET_SIZE) {
                h->read_buffer = realloc(h->read_buffer, h->max_packet);
                h->write_buffer = realloc(h->write_buffer, h->max_packet);
-               if (h->read_buffer == 0 || h->write_buffer == 0) {
+               if (!h->read_buffer || !h->write_buffer) {
                        LOG_ERROR("unable to reallocate memory");
                        return ERROR_FAIL;
                }
@@ -664,7 +664,7 @@ static int icdi_usb_open(struct hl_interface_param_s *param, void **fd)
 
        h = calloc(1, sizeof(struct icdi_usb_handle_s));
 
-       if (h == 0) {
+       if (!h) {
                LOG_ERROR("unable to allocate memory");
                return ERROR_FAIL;
        }
@@ -712,7 +712,7 @@ static int icdi_usb_open(struct hl_interface_param_s *param, void **fd)
        h->write_buffer = malloc(ICDI_PACKET_SIZE);
        h->max_packet = ICDI_PACKET_SIZE;
 
-       if (h->read_buffer == 0 || h->write_buffer == 0) {
+       if (!h->read_buffer || !h->write_buffer) {
                LOG_DEBUG("malloc failed");
                goto error_open;
        }
index 0c0f24ee2bb66667213c6ac42d463a7aeea3c166..5d41656b4f563719a0787e1ef9297ed5fceecb33 100644 (file)
@@ -148,7 +148,7 @@ static int usbprog_init(void)
        usbprog_jtag_handle = usbprog_jtag_open();
 
        tms_chain_index = 0;
-       if (usbprog_jtag_handle == 0) {
+       if (!usbprog_jtag_handle) {
                LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
        }
index 8e5d638db1c0e78961e8d91e339b4ebef37676be..3ea98ad6b19d9d7a607bf2c9d53171f8262e08ce 100644 (file)
@@ -1300,7 +1300,7 @@ static int xds110_swd_run_queue(void)
 
        /* Transfer results into caller's buffers */
        for (result = 0; result < xds110.txn_result_count; result++)
-               if (xds110.txn_dap_results[result] != 0)
+               if (xds110.txn_dap_results[result])
                        *xds110.txn_dap_results[result] = dap_results[result];
 
        xds110.txn_request_size = 0;
@@ -1611,7 +1611,7 @@ static void xds110_flush(void)
                        }
                        bits = 0;
                }
-               if (xds110.txn_scan_results[result].buffer != 0)
+               if (xds110.txn_scan_results[result].buffer)
                        bit_copy(xds110.txn_scan_results[result].buffer, 0, data_pntr,
                                bits, xds110.txn_scan_results[result].num_bits);
                bits += xds110.txn_scan_results[result].num_bits;
@@ -1687,8 +1687,8 @@ static void xds110_execute_pathmove(struct jtag_command *cmd)
        if (num_states == 0)
                return;
 
-       path = (uint8_t *)malloc(num_states * sizeof(uint8_t));
-       if (path == 0) {
+       path = malloc(num_states * sizeof(uint8_t));
+       if (path) {
                LOG_ERROR("XDS110: unable to allocate memory");
                return;
        }
@@ -1766,7 +1766,7 @@ static void xds110_queue_scan(struct jtag_command *cmd)
        /* Clear data out buffer to default value of all zeros */
        memset((void *)buffer, 0x00, total_bytes);
        for (i = 0; i < cmd->cmd.scan->num_fields; i++) {
-               if (cmd->cmd.scan->fields[i].out_value != 0) {
+               if (cmd->cmd.scan->fields[i].out_value) {
                        /* Copy over data to scan out into request buffer */
                        bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0,
                                cmd->cmd.scan->fields[i].num_bits);

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)