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

@@ -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;
}