openocd: avoid checking for non NULL pointer to free it 08/5808/5
authorAntonio Borneo <borneo.antonio@gmail.com>
Sun, 16 Aug 2020 19:35:10 +0000 (21:35 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 5 Sep 2020 16:11:50 +0000 (17:11 +0100)
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
src/helper/ioutil.c
src/rtos/chibios.c
src/rtos/linux.c
src/rtos/rtos.c
src/server/gdb_server.c
src/server/server.c
src/server/telnet_server.c
src/svf/svf.c
src/xsvf/xsvf.c

index c103ce173f73c97da3afe91730a1c6a0f2622418..ffdeca898f4ea7fc266b4e6fb5f537578a623612 100644 (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);
 
index 4d2b1b2d788a3ab48ec8758756cf65615da211f6..a56d3ce0581ee2f3c28a65492ea0ec614197aa79 100644 (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) {
index cd1ed218abfb996f712e04f534acc42a3f670e84..dbbf97b4467ae2a3de26c6345b7cb6783a97b288 100644 (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;
        }
index 97ce255b924c386ed82458c8cb8421bce307585c..62b65aae12e66271b48f6c5827babf22b282b1a1 100644 (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;
 }
index f94b7281729a1c867939da3ba289fd824166c7c6..85d3c14b12e2bc0b02bb37f154ac2e2344d5eed9 100644 (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;
                        }
index d96f0b6cf61b765b34ba22e06aec828c70640bb8..3b55d0d7caaf5e8b82fe0e353f3cb54562f9496e 100644 (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);
 
index d0583a9b3e22f36bd2a8007e689491f0c3a2409a..0243c6328eb8205236eaaea6dbb7d3ee3edc4911 100644 (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;
 }
index 01059207625d5e1653430016d5881f6b8bfccda9..b62cdae08feca1beda1f70aea5bd9e8b2bf20c84 100644 (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;
 }
index 53779bb1cd6bf9698b59a13db6e430c16e2e7258..eaa5a3aae139dd485a1ba03cf989cf5af9c02d79 100644 (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);
 

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)