jtag: avoid checking for non NULL pointer to free it

The function free() can be called with a NULL pointer as argument,
no need to check the argument before. If the pointer is NULL, no
operation is performed by free().

Remove the occurrences of pattern:
	if (ptr)
		free(ptr);

Change-Id: I2938e333bd1eac5218bd67aefb9d8f373da017a8
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5810
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2020-08-17 10:05:11 +02:00
parent a098a14eb5
commit 24db985f60
23 changed files with 60 additions and 122 deletions

View File

@ -2537,15 +2537,9 @@ static int aice_usb_close(void)
{
jtag_libusb_close(aice_handler.usb_handle);
if (custom_srst_script)
free(custom_srst_script);
if (custom_trst_script)
free(custom_trst_script);
if (custom_restart_script)
free(custom_restart_script);
free(custom_srst_script);
free(custom_trst_script);
free(custom_restart_script);
return ERROR_OK;
}

View File

@ -370,8 +370,7 @@ static int amt_jtagaccel_execute_queue(void)
amt_jtagaccel_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
retval = ERROR_JTAG_QUEUE_FAILED;
if (buffer)
free(buffer);
free(buffer);
break;
case JTAG_SLEEP:
LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);

View File

@ -664,8 +664,7 @@ static int armjtagew_tap_execute(void)
return ERROR_JTAG_QUEUE_FAILED;
}
if (pending_scan_result->buffer != NULL)
free(pending_scan_result->buffer);
free(pending_scan_result->buffer);
}
} else {
LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d",

View File

@ -358,8 +358,7 @@ int bitbang_execute_queue(void)
return ERROR_FAIL;
if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
retval = ERROR_JTAG_QUEUE_FAILED;
if (buffer)
free(buffer);
free(buffer);
break;
case JTAG_SLEEP:
LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);

View File

@ -352,10 +352,8 @@ static int buspirate_quit(void)
buspirate_serial_close(buspirate_fd);
if (buspirate_port) {
free(buspirate_port);
buspirate_port = NULL;
}
free(buspirate_port);
buspirate_port = NULL;
return ERROR_OK;
}

View File

@ -886,8 +886,7 @@ static int syncbb_execute_queue(void)
syncbb_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
retval = ERROR_JTAG_QUEUE_FAILED;
if (buffer)
free(buffer);
free(buffer);
break;
case JTAG_SLEEP:

View File

@ -709,8 +709,7 @@ static int ftdi_quit(void)
COMMAND_HANDLER(ftdi_handle_device_desc_command)
{
if (CMD_ARGC == 1) {
if (ftdi_device_desc)
free(ftdi_device_desc);
free(ftdi_device_desc);
ftdi_device_desc = strdup(CMD_ARGV[0]);
} else {
LOG_ERROR("expected exactly one argument to ftdi_device_desc <description>");
@ -722,8 +721,7 @@ COMMAND_HANDLER(ftdi_handle_device_desc_command)
COMMAND_HANDLER(ftdi_handle_serial_command)
{
if (CMD_ARGC == 1) {
if (ftdi_serial)
free(ftdi_serial);
free(ftdi_serial);
ftdi_serial = strdup(CMD_ARGV[0]);
} else {
return ERROR_COMMAND_SYNTAX_ERROR;

View File

@ -327,8 +327,7 @@ static int gw16012_execute_queue(void)
gw16012_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
retval = ERROR_JTAG_QUEUE_FAILED;
if (buffer)
free(buffer);
free(buffer);
break;
case JTAG_SLEEP:
LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);

View File

@ -23,8 +23,7 @@ void jtag_usb_set_location(const char *location)
JTAG_USB_MAX_LOCATION_LENGTH)
LOG_WARNING("usb location string is too long!!\n");
if (jtag_usb_location)
free(jtag_usb_location);
free(jtag_usb_location);
jtag_usb_location = strndup(location, JTAG_USB_MAX_LOCATION_LENGTH);
}

View File

@ -439,8 +439,7 @@ static int jtag_vpi_scan(struct scan_command *cmd)
if (retval != ERROR_OK)
return retval;
if (buf)
free(buf);
free(buf);
if (cmd->end_state != TAP_DRSHIFT) {
retval = jtag_vpi_state_move(cmd->end_state);

View File

@ -227,18 +227,11 @@ static int kitprog_quit(void)
{
kitprog_usb_close();
if (kitprog_handle->packet_buffer != NULL)
free(kitprog_handle->packet_buffer);
if (kitprog_handle->serial != NULL)
free(kitprog_handle->serial);
if (kitprog_handle != NULL)
free(kitprog_handle);
if (kitprog_serial != NULL)
free(kitprog_serial);
if (pending_transfers != NULL)
free(pending_transfers);
free(kitprog_handle->packet_buffer);
free(kitprog_handle->serial);
free(kitprog_handle);
free(kitprog_serial);
free(pending_transfers);
return ERROR_OK;
}

View File

@ -404,13 +404,10 @@ void mpsse_close(struct mpsse_ctx *ctx)
if (ctx->usb_ctx)
libusb_exit(ctx->usb_ctx);
bit_copy_discard(&ctx->read_queue);
if (ctx->write_buffer)
free(ctx->write_buffer);
if (ctx->read_buffer)
free(ctx->read_buffer);
if (ctx->read_chunk)
free(ctx->read_chunk);
free(ctx->write_buffer);
free(ctx->read_buffer);
free(ctx->read_chunk);
free(ctx);
}

View File

@ -386,25 +386,17 @@ static int opendous_quit(void)
{
opendous_usb_close(opendous_jtag_handle);
if (usb_out_buffer) {
free(usb_out_buffer);
usb_out_buffer = NULL;
}
free(usb_out_buffer);
usb_out_buffer = NULL;
if (usb_in_buffer) {
free(usb_in_buffer);
usb_in_buffer = NULL;
}
free(usb_in_buffer);
usb_in_buffer = NULL;
if (pending_scan_results_buffer) {
free(pending_scan_results_buffer);
pending_scan_results_buffer = NULL;
}
free(pending_scan_results_buffer);
pending_scan_results_buffer = NULL;
if (opendous_type) {
free(opendous_type);
opendous_type = NULL;
}
free(opendous_type);
opendous_type = NULL;
return ERROR_OK;
}
@ -697,8 +689,7 @@ int opendous_tap_execute(void)
return ERROR_JTAG_QUEUE_FAILED;
}
if (pending_scan_result->buffer != NULL)
free(pending_scan_result->buffer);
free(pending_scan_result->buffer);
}
opendous_tap_init();

View File

@ -591,8 +591,7 @@ static int openjtag_execute_tap_queue(void)
#endif
jtag_read_buffer(buffer, openjtag_scan_result_buffer[res_count].command);
if (openjtag_scan_result_buffer[res_count].buffer)
free(openjtag_scan_result_buffer[res_count].buffer);
free(openjtag_scan_result_buffer[res_count].buffer);
res_count++;
}

View File

@ -392,10 +392,8 @@ static int parport_quit(void)
parport_write_data();
}
if (parport_cable) {
free(parport_cable);
parport_cable = NULL;
}
free(parport_cable);
parport_cable = NULL;
return ERROR_OK;
}

View File

@ -511,8 +511,7 @@ static char *presto_serial;
COMMAND_HANDLER(presto_handle_serial_command)
{
if (CMD_ARGC == 1) {
if (presto_serial)
free(presto_serial);
free(presto_serial);
presto_serial = strdup(CMD_ARGV[0]);
} else
return ERROR_COMMAND_SYNTAX_ERROR;
@ -553,10 +552,8 @@ static int presto_jtag_quit(void)
presto_close();
LOG_INFO("PRESTO closed");
if (presto_serial) {
free(presto_serial);
presto_serial = NULL;
}
free(presto_serial);
presto_serial = NULL;
return ERROR_OK;
}

View File

@ -662,14 +662,9 @@ static int icdi_usb_close(void *handle)
if (h->usb_ctx)
libusb_exit(h->usb_ctx);
if (h->read_buffer)
free(h->read_buffer);
if (h->write_buffer)
free(h->write_buffer);
free(h->read_buffer);
free(h->write_buffer);
free(handle);
return ERROR_OK;
}

View File

@ -736,8 +736,7 @@ static int ublast_scan(struct scan_command *cmd)
ublast_queue_tdi(buf, scan_bits, type);
ret = jtag_read_buffer(buf, cmd);
if (buf)
free(buf);
free(buf);
/*
* ublast_queue_tdi sends the last bit with TMS=1. We are therefore
* already in Exit1-DR/IR and have to skip the first step on our way

View File

@ -137,8 +137,7 @@ static int usbprog_execute_queue(void)
usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
return ERROR_JTAG_QUEUE_FAILED;
if (buffer)
free(buffer);
free(buffer);
break;
case JTAG_SLEEP:
LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);

View File

@ -299,14 +299,11 @@ RESULT versaloon_fini(void)
versaloon_usb_device_handle = NULL;
if (versaloon_buf != NULL) {
free(versaloon_buf);
versaloon_buf = NULL;
}
if (versaloon_cmd_buf != NULL) {
free(versaloon_cmd_buf);
versaloon_cmd_buf = NULL;
}
free(versaloon_buf);
versaloon_buf = NULL;
free(versaloon_cmd_buf);
versaloon_cmd_buf = NULL;
}
return ERROR_OK;

View File

@ -245,18 +245,14 @@ static int vsllink_speed_div(int jtag_speed, int *khz)
static void vsllink_free_buffer(void)
{
if (tdi_buffer != NULL) {
free(tdi_buffer);
tdi_buffer = NULL;
}
if (tdo_buffer != NULL) {
free(tdo_buffer);
tdo_buffer = NULL;
}
if (tms_buffer != NULL) {
free(tms_buffer);
tms_buffer = NULL;
}
free(tdi_buffer);
tdi_buffer = NULL;
free(tdo_buffer);
tdo_buffer = NULL;
free(tms_buffer);
tms_buffer = NULL;
}
static int vsllink_quit(void)
@ -676,8 +672,7 @@ static int vsllink_jtag_execute(void)
return ERROR_JTAG_QUEUE_FAILED;
}
if (pending_scan_result->buffer != NULL)
free(pending_scan_result->buffer);
free(pending_scan_result->buffer);
}
}
} else {

View File

@ -282,8 +282,7 @@ static int xlnx_pcie_xvc_execute_scan(struct jtag_command *cmd)
};
err = jtag_read_buffer(buf, cmd->cmd.scan);
if (buf)
free(buf);
free(buf);
if (tap_get_state() != tap_get_end_state())
err = xlnx_pcie_xvc_execute_statemove(1);
@ -291,8 +290,7 @@ static int xlnx_pcie_xvc_execute_scan(struct jtag_command *cmd)
return err;
out_err:
if (buf)
free(buf);
free(buf);
return err;
}
@ -456,8 +454,7 @@ COMMAND_HANDLER(xlnx_pcie_xvc_handle_config_command)
* limit the memory we're leaking by freeing the old one first
* before allocating a new one ...
*/
if (xlnx_pcie_xvc->device)
free(xlnx_pcie_xvc->device);
free(xlnx_pcie_xvc->device);
xlnx_pcie_xvc->device = strdup(CMD_ARGV[0]);
return ERROR_OK;

View File

@ -1155,10 +1155,8 @@ COMMAND_HANDLER(handle_irscan_command)
retval = jtag_execute_queue();
error_return:
for (i = 0; i < num_fields; i++) {
if (NULL != fields[i].out_value)
free((void *)fields[i].out_value);
}
for (i = 0; i < num_fields; i++)
free((void *)fields[i].out_value);
free(fields);