openocd: 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);

While there replace a sequence malloc(size)+memset(,0,size) with a
calloc(1,size).
Replace a pointer assignment to '0' with an assignment to NULL.
In server/*, an error is logged if the ptr was already NULL. This
cannot happen since the pointer was already referenced few lines
before and openocd would have been already SIGSEGV in that case,
so remove the log.

Change-Id: I10822029fe8390b59edff4070575bf7f754e44ac
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5808
Reviewed-by: Adrian M Negreanu <adrian.negreanu@nxp.com>
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2020-08-16 21:35:10 +02:00
parent 62329444ab
commit 4e98d44fd1
9 changed files with 61 additions and 111 deletions

View File

@ -230,8 +230,7 @@ COMMAND_HANDLER(handle_cp_command)
else
command_print(CMD, "copy failed");
if (data != NULL)
free(data);
free(data);
if (f != NULL)
fclose(f);

View File

@ -150,10 +150,8 @@ static int chibios_update_memory_signature(struct rtos *rtos)
param = (struct chibios_params *) rtos->rtos_specific_params;
/* Free existing memory description.*/
if (param->signature) {
free(param->signature);
param->signature = 0;
}
free(param->signature);
param->signature = NULL;
signature = malloc(sizeof(*signature));
if (!signature) {

View File

@ -627,8 +627,7 @@ struct threads *liste_del_task(struct threads *task_list, struct threads **t,
task_list = (*t)->next;
/* free content of threads */
if ((*t)->context)
free((*t)->context);
free((*t)->context);
free(*t);
*t = prev ? prev : task_list;
@ -781,8 +780,7 @@ static int clean_threadlist(struct target *target)
while (temp != NULL) {
old = temp;
if (temp->context)
free(temp->context);
free(temp->context);
temp = temp->next;
free(old);
@ -931,10 +929,8 @@ static int linux_task_update(struct target *target, int context)
while (thread_list != NULL) {
thread_list->status = 0; /*setting all tasks to dead state*/
if (thread_list->context) {
free(thread_list->context);
thread_list->context = NULL;
}
free(thread_list->context);
thread_list->context = NULL;
thread_list = thread_list->next;
}

View File

@ -100,9 +100,7 @@ static void os_free(struct target *target)
if (!target->rtos)
return;
if (target->rtos->symbols)
free(target->rtos->symbols);
free(target->rtos->symbols);
free(target->rtos);
target->rtos = NULL;
}
@ -646,10 +644,9 @@ int rtos_try_next(struct target *target)
return 0;
os->type = *type;
if (os->symbols) {
free(os->symbols);
os->symbols = NULL;
}
free(os->symbols);
os->symbols = NULL;
return 1;
}

View File

@ -1067,11 +1067,8 @@ static int gdb_connection_closed(struct connection *connection)
/* if this connection registered a debug-message receiver delete it */
delete_debug_msg_receiver(connection->cmd_ctx, target);
if (connection->priv) {
free(connection->priv);
connection->priv = NULL;
} else
LOG_ERROR("BUG: connection->priv == NULL");
free(connection->priv);
connection->priv = NULL;
target_unregister_event_callback(gdb_target_callback_event_handler, connection);
@ -1758,8 +1755,7 @@ static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(
char *t = *xml;
*xml = realloc(*xml, *size);
if (*xml == NULL) {
if (t)
free(t);
free(t);
*retval = ERROR_SERVER_REMOTE_CLOSED;
return;
}

View File

@ -403,19 +403,14 @@ static int remove_services(void)
remove_connections(c);
if (c->name)
free(c->name);
free(c->name);
if (c->type == CONNECTION_PIPE) {
if (c->fd != -1)
close(c->fd);
}
if (c->port)
free(c->port);
if (c->priv)
free(c->priv);
free(c->port);
free(c->priv);
/* delete service */
free(c);

View File

@ -451,8 +451,7 @@ static int telnet_input(struct connection *connection)
if (*t_con->line && (prev_line == NULL ||
strcmp(t_con->line, prev_line))) {
/* if the history slot is already taken, free it */
if (t_con->history[t_con->next_history])
free(t_con->history[t_con->next_history]);
free(t_con->history[t_con->next_history]);
/* add line to history */
t_con->history[t_con->next_history] = strdup(t_con->line);
@ -465,8 +464,7 @@ static int telnet_input(struct connection *connection)
t_con->current_history =
t_con->next_history;
if (t_con->history[t_con->current_history])
free(t_con->history[t_con->current_history]);
free(t_con->history[t_con->current_history]);
t_con->history[t_con->current_history] = strdup("");
}
@ -647,29 +645,22 @@ static int telnet_connection_closed(struct connection *connection)
log_remove_callback(telnet_log_callback, connection);
if (t_con->prompt) {
free(t_con->prompt);
t_con->prompt = NULL;
}
free(t_con->prompt);
t_con->prompt = NULL;
/* save telnet history */
telnet_save_history(t_con);
for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++) {
if (t_con->history[i]) {
free(t_con->history[i]);
t_con->history[i] = NULL;
}
free(t_con->history[i]);
t_con->history[i] = NULL;
}
/* if this connection registered a debug-message receiver delete it */
delete_debug_msg_receiver(connection->cmd_ctx, NULL);
if (connection->priv) {
free(connection->priv);
connection->priv = NULL;
} else
LOG_ERROR("BUG: connection->priv == NULL");
free(connection->priv);
connection->priv = NULL;
return ERROR_OK;
}

View File

@ -302,22 +302,17 @@ static int svf_realloc_buffers(size_t len)
static void svf_free_xxd_para(struct svf_xxr_para *para)
{
if (NULL != para) {
if (para->tdi != NULL) {
free(para->tdi);
para->tdi = NULL;
}
if (para->tdo != NULL) {
free(para->tdo);
para->tdo = NULL;
}
if (para->mask != NULL) {
free(para->mask);
para->mask = NULL;
}
if (para->smask != NULL) {
free(para->smask);
para->smask = NULL;
}
free(para->tdi);
para->tdi = NULL;
free(para->tdo);
para->tdo = NULL;
free(para->mask);
para->mask = NULL;
free(para->smask);
para->smask = NULL;
}
}
@ -546,28 +541,23 @@ free_all:
svf_fd = 0;
/* free buffers */
if (svf_command_buffer) {
free(svf_command_buffer);
svf_command_buffer = NULL;
svf_command_buffer_size = 0;
}
if (svf_check_tdo_para) {
free(svf_check_tdo_para);
svf_check_tdo_para = NULL;
svf_check_tdo_para_index = 0;
}
if (svf_tdi_buffer) {
free(svf_tdi_buffer);
svf_tdi_buffer = NULL;
}
if (svf_tdo_buffer) {
free(svf_tdo_buffer);
svf_tdo_buffer = NULL;
}
if (svf_mask_buffer) {
free(svf_mask_buffer);
svf_mask_buffer = NULL;
}
free(svf_command_buffer);
svf_command_buffer = NULL;
svf_command_buffer_size = 0;
free(svf_check_tdo_para);
svf_check_tdo_para = NULL;
svf_check_tdo_para_index = 0;
free(svf_tdi_buffer);
svf_tdi_buffer = NULL;
free(svf_tdo_buffer);
svf_tdo_buffer = NULL;
free(svf_mask_buffer);
svf_mask_buffer = NULL;
svf_buffer_index = 0;
svf_buffer_size = 0;
@ -771,16 +761,12 @@ static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_
int new_byte_len = (new_bit_len + 7) >> 3;
if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
if (*arr != NULL) {
free(*arr);
*arr = NULL;
}
*arr = malloc(new_byte_len);
free(*arr);
*arr = calloc(1, new_byte_len);
if (NULL == *arr) {
LOG_ERROR("not enough memory");
return ERROR_FAIL;
}
memset(*arr, 0, new_byte_len);
}
return ERROR_OK;
}

View File

@ -411,12 +411,9 @@ COMMAND_HANDLER(handle_xsvf_command)
xsdrsize = be_to_h_u32(xsdrsize_buf);
LOG_DEBUG("XSDRSIZE %d", xsdrsize);
if (dr_out_buf)
free(dr_out_buf);
if (dr_in_buf)
free(dr_in_buf);
if (dr_in_mask)
free(dr_in_mask);
free(dr_out_buf);
free(dr_in_buf);
free(dr_in_mask);
dr_out_buf = malloc((xsdrsize + 7) / 8);
dr_in_buf = malloc((xsdrsize + 7) / 8);
@ -1027,14 +1024,9 @@ COMMAND_HANDLER(handle_xsvf_command)
return ERROR_FAIL;
}
if (dr_out_buf)
free(dr_out_buf);
if (dr_in_buf)
free(dr_in_buf);
if (dr_in_mask)
free(dr_in_mask);
free(dr_out_buf);
free(dr_in_buf);
free(dr_in_mask);
close(xsvf_fd);