jtag: drivers: with pointers, use NULL instead of 0

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
This commit is contained in:
Antonio Borneo 2023-04-08 23:21:33 +02:00
parent 411dfa2409
commit 07e1ebcc12
14 changed files with 40 additions and 32 deletions

View File

@ -213,7 +213,7 @@ static int armjtagew_init(void)
armjtagew_handle = armjtagew_usb_open(); armjtagew_handle = armjtagew_usb_open();
if (armjtagew_handle == 0) { if (!armjtagew_handle) {
LOG_ERROR( LOG_ERROR(
"Cannot find ARM-JTAG-EW Interface! Please check connection and permissions."); "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;

View File

@ -107,7 +107,7 @@ static int at91rm9200_quit(void);
static struct bitbang_interface at91rm9200_bitbang = { static struct bitbang_interface at91rm9200_bitbang = {
.read = at91rm9200_read, .read = at91rm9200_read,
.write = at91rm9200_write, .write = at91rm9200_write,
.blink = 0 .blink = NULL,
}; };
static bb_value_t at91rm9200_read(void) static bb_value_t at91rm9200_read(void)
@ -157,8 +157,12 @@ COMMAND_HANDLER(at91rm9200_handle_device_command)
return ERROR_COMMAND_SYNTAX_ERROR; return ERROR_COMMAND_SYNTAX_ERROR;
/* only if the device name wasn't overwritten by cmdline */ /* 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)); 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]); strcpy(at91rm9200_device, CMD_ARGV[0]);
} }

View File

@ -58,7 +58,7 @@ struct adapter_driver ep93xx_adapter_driver = {
static struct bitbang_interface ep93xx_bitbang = { static struct bitbang_interface ep93xx_bitbang = {
.read = ep93xx_read, .read = ep93xx_read,
.write = ep93xx_write, .write = ep93xx_write,
.blink = 0, .blink = NULL,
}; };
static bb_value_t ep93xx_read(void) static bb_value_t ep93xx_read(void)

View File

@ -235,7 +235,7 @@ static int ft232r_speed(int divisor)
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, 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"); LOG_ERROR("cannot set baud rate");
return ERROR_JTAG_DEVICE_ERROR; return ERROR_JTAG_DEVICE_ERROR;
} }
@ -266,7 +266,7 @@ static int ft232r_init(void)
/* Reset the device. */ /* Reset the device. */
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, 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"); LOG_ERROR("unable to reset device");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }
@ -275,7 +275,7 @@ static int ft232r_init(void)
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, 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, 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"); LOG_ERROR("cannot set sync bitbang mode");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }
@ -288,13 +288,13 @@ static int ft232r_init(void)
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
SIO_SET_BAUD_RATE, divisor, SIO_SET_BAUD_RATE, divisor,
0, 0, 0, 1000) != 0) { 0, NULL, 0, 1000) != 0) {
LOG_ERROR("cannot set baud rate"); LOG_ERROR("cannot set baud rate");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, 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"); LOG_ERROR("unable to set latency timer");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }
@ -315,7 +315,7 @@ static int ft232r_quit(void)
if (jtag_libusb_control_transfer(adapter, if (jtag_libusb_control_transfer(adapter,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
SIO_SET_BITMODE, ft232r_restore_bitmode, 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"); LOG_ERROR("cannot set bitmode to restore serial port");
} }
} }

View File

@ -181,7 +181,7 @@ static int ftdi_set_signal(const struct signal *s, char value)
oe = s->invert_oe; oe = s->invert_oe;
break; break;
default: default:
assert(0 && "invalid signal level specifier"); LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value, value);
return ERROR_FAIL; return ERROR_FAIL;
} }

View File

@ -313,7 +313,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const
int err; int err;
if (!ctx) if (!ctx)
return 0; return NULL;
bit_copy_queue_init(&ctx->read_queue); bit_copy_queue_init(&ctx->read_queue);
ctx->read_chunk_size = 16384; 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 : "*", description ? description : "*",
serial ? serial : "*", serial ? serial : "*",
location ? location : "*"); location ? location : "*");
ctx->usb_dev = 0; ctx->usb_dev = NULL;
goto error; goto error;
} }
@ -378,7 +378,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const
return ctx; return ctx;
error: error:
mpsse_close(ctx); mpsse_close(ctx);
return 0; return NULL;
} }
void mpsse_close(struct mpsse_ctx *ctx) 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, void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
unsigned length, uint8_t mode) 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, void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_offset, unsigned length,
uint8_t mode) 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, 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, void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
unsigned length, bool tdi, uint8_t mode) 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, 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) if (ctx->write_count == 0)
return retval; return retval;
struct libusb_transfer *read_transfer = 0; struct libusb_transfer *read_transfer = NULL;
struct transfer_result read_result = { .ctx = ctx, .done = true }; struct transfer_result read_result = { .ctx = ctx, .done = true };
if (ctx->read_count) { if (ctx->read_count) {
buffer_write_byte(ctx, 0x87); /* SEND_IMMEDIATE */ buffer_write_byte(ctx, 0x87); /* SEND_IMMEDIATE */

View File

@ -347,7 +347,7 @@ static int opendous_init(void)
opendous_jtag_handle = opendous_usb_open(); 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."); LOG_ERROR("Cannot find opendous Interface! Please check connection and permissions.");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }

View File

@ -411,9 +411,13 @@ COMMAND_HANDLER(parport_handle_parport_cable_command)
return ERROR_OK; return ERROR_OK;
/* only if the cable name wasn't overwritten by cmdline */ /* 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[] ... */ /* REVISIT first verify that it's listed in cables[] ... */
parport_cable = malloc(strlen(CMD_ARGV[0]) + sizeof(char)); 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]); strcpy(parport_cable, CMD_ARGV[0]);
} }

View File

@ -632,7 +632,7 @@ static int dtc_queue_run(void)
uint8_t dtc_mask, tdo_mask; uint8_t dtc_mask, tdo_mask;
uint8_t reply_buffer[USB_EP2IN_SIZE]; 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.cmd_index < USB_EP2BANK_SIZE);
assert(dtc_queue.reply_index <= USB_EP2IN_SIZE); assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);

View 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)); h = calloc(1, sizeof(struct stlink_usb_handle_s));
if (h == 0) { if (!h) {
LOG_DEBUG("malloc failed"); LOG_DEBUG("malloc failed");
return ERROR_FAIL; return ERROR_FAIL;
} }

View File

@ -571,7 +571,7 @@ static struct bitbang_interface sysfsgpio_bitbang = {
.swdio_read = sysfsgpio_swdio_read, .swdio_read = sysfsgpio_swdio_read,
.swdio_drive = sysfsgpio_swdio_drive, .swdio_drive = sysfsgpio_swdio_drive,
.swd_write = sysfsgpio_swd_write, .swd_write = sysfsgpio_swd_write,
.blink = 0 .blink = NULL,
}; };
/* helper func to close and cleanup files only if they were valid/ used */ /* helper func to close and cleanup files only if they were valid/ used */

View File

@ -369,7 +369,7 @@ static int icdi_usb_query(void *handle)
if (h->max_packet != ICDI_PACKET_SIZE) { if (h->max_packet != ICDI_PACKET_SIZE) {
h->read_buffer = realloc(h->read_buffer, h->max_packet); h->read_buffer = realloc(h->read_buffer, h->max_packet);
h->write_buffer = realloc(h->write_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"); LOG_ERROR("unable to reallocate memory");
return ERROR_FAIL; 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)); h = calloc(1, sizeof(struct icdi_usb_handle_s));
if (h == 0) { if (!h) {
LOG_ERROR("unable to allocate memory"); LOG_ERROR("unable to allocate memory");
return ERROR_FAIL; 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->write_buffer = malloc(ICDI_PACKET_SIZE);
h->max_packet = 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"); LOG_DEBUG("malloc failed");
goto error_open; goto error_open;
} }

View File

@ -148,7 +148,7 @@ static int usbprog_init(void)
usbprog_jtag_handle = usbprog_jtag_open(); usbprog_jtag_handle = usbprog_jtag_open();
tms_chain_index = 0; 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."); LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
return ERROR_JTAG_INIT_FAILED; return ERROR_JTAG_INIT_FAILED;
} }

View File

@ -1300,7 +1300,7 @@ static int xds110_swd_run_queue(void)
/* Transfer results into caller's buffers */ /* Transfer results into caller's buffers */
for (result = 0; result < xds110.txn_result_count; result++) 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_dap_results[result] = dap_results[result];
xds110.txn_request_size = 0; xds110.txn_request_size = 0;
@ -1611,7 +1611,7 @@ static void xds110_flush(void)
} }
bits = 0; 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, 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);
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) if (num_states == 0)
return; return;
path = (uint8_t *)malloc(num_states * sizeof(uint8_t)); path = malloc(num_states * sizeof(uint8_t));
if (path == 0) { if (path) {
LOG_ERROR("XDS110: unable to allocate memory"); LOG_ERROR("XDS110: unable to allocate memory");
return; return;
} }
@ -1766,7 +1766,7 @@ static void xds110_queue_scan(struct jtag_command *cmd)
/* Clear data out buffer to default value of all zeros */ /* Clear data out buffer to default value of all zeros */
memset((void *)buffer, 0x00, total_bytes); memset((void *)buffer, 0x00, total_bytes);
for (i = 0; i < cmd->cmd.scan->num_fields; i++) { 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 */ /* Copy over data to scan out into request buffer */
bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0, bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0,
cmd->cmd.scan->fields[i].num_bits); cmd->cmd.scan->fields[i].num_bits);