target: avoid checking for non NULL pointer to free it 09/5809/3 master
authorAntonio Borneo <borneo.antonio@gmail.com>
Mon, 17 Aug 2020 07:58:58 +0000 (09:58 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 5 Sep 2020 19:47:11 +0000 (20:47 +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);

In target/openrisc/jsp_server.c, 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: I290a32e6d4deab167676af4ddc83523c830ae49e
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5809
Tested-by: jenkins
src/target/image.c
src/target/mips32_pracc.c
src/target/mips_m4k.c
src/target/nds32.c
src/target/openrisc/jsp_server.c
src/target/openrisc/or1k.c
src/target/openrisc/or1k_du_adv.c
src/target/riscv/riscv.c
src/target/target.c
src/target/trace.c
src/target/xscale.c

index 64d4d52..8160e5f 100644 (file)
@@ -984,40 +984,30 @@ void image_close(struct image *image)
 
                fileio_close(image_ihex->fileio);
 
-               if (image_ihex->buffer) {
-                       free(image_ihex->buffer);
-                       image_ihex->buffer = NULL;
-               }
+               free(image_ihex->buffer);
+               image_ihex->buffer = NULL;
        } else if (image->type == IMAGE_ELF) {
                struct image_elf *image_elf = image->type_private;
 
                fileio_close(image_elf->fileio);
 
-               if (image_elf->header) {
-                       free(image_elf->header);
-                       image_elf->header = NULL;
-               }
+               free(image_elf->header);
+               image_elf->header = NULL;
 
-               if (image_elf->segments) {
-                       free(image_elf->segments);
-                       image_elf->segments = NULL;
-               }
+               free(image_elf->segments);
+               image_elf->segments = NULL;
        } else if (image->type == IMAGE_MEMORY) {
                struct image_memory *image_memory = image->type_private;
 
-               if (image_memory->cache) {
-                       free(image_memory->cache);
-                       image_memory->cache = NULL;
-               }
+               free(image_memory->cache);
+               image_memory->cache = NULL;
        } else if (image->type == IMAGE_SRECORD) {
                struct image_mot *image_mot = image->type_private;
 
                fileio_close(image_mot->fileio);
 
-               if (image_mot->buffer) {
-                       free(image_mot->buffer);
-                       image_mot->buffer = NULL;
-               }
+               free(image_mot->buffer);
+               image_mot->buffer = NULL;
        } else if (image->type == IMAGE_BUILDER) {
                int i;
 
@@ -1027,15 +1017,11 @@ void image_close(struct image *image)
                }
        }
 
-       if (image->type_private) {
-               free(image->type_private);
-               image->type_private = NULL;
-       }
+       free(image->type_private);
+       image->type_private = NULL;
 
-       if (image->sections) {
-               free(image->sections);
-               image->sections = NULL;
-       }
+       free(image->sections);
+       image->sections = NULL;
 }
 
 int image_calculate_checksum(uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
index 81be70d..9bac40e 100644 (file)
@@ -346,8 +346,7 @@ void pracc_add_li32(struct pracc_queue_info *ctx, uint32_t reg_num, uint32_t dat
 
 inline void pracc_queue_free(struct pracc_queue_info *ctx)
 {
-       if (ctx->pracc_list != NULL)
-               free(ctx->pracc_list);
+       free(ctx->pracc_list);
 }
 
 int mips32_pracc_queue_exec(struct mips_ejtag *ejtag_info, struct pracc_queue_info *ctx,
@@ -550,8 +549,7 @@ int mips32_pracc_read_mem(struct mips_ejtag *ejtag_info, uint32_t addr, int size
        }
 exit:
        pracc_queue_free(&ctx);
-       if (data != NULL)
-               free(data);
+       free(data);
        return ctx.retval;
 }
 
index 85eea93..52b4b32 100644 (file)
@@ -1072,7 +1072,7 @@ static int mips_m4k_read_memory(struct target *target, target_addr_t address,
                }
        }
 
-       if ((size > 1) && (t != NULL))
+       if (size > 1)
                free(t);
 
        return retval;
@@ -1135,8 +1135,7 @@ static int mips_m4k_write_memory(struct target *target, target_addr_t address,
        else
                retval = mips32_dmaacc_write_mem(ejtag_info, address, size, count, buffer);
 
-       if (t != NULL)
-               free(t);
+       free(t);
 
        if (ERROR_OK != retval)
                return retval;
@@ -1261,8 +1260,7 @@ static int mips_m4k_bulk_write_memory(struct target *target, target_addr_t addre
        retval = mips32_pracc_fastdata_xfer(ejtag_info, mips32->fast_data_area, write_t, address,
                        count, t);
 
-       if (t != NULL)
-               free(t);
+       free(t);
 
        if (retval != ERROR_OK)
                LOG_ERROR("Fastdata access Failed");
index 0d1a23a..487e19c 100644 (file)
@@ -1462,8 +1462,7 @@ int nds32_add_software_breakpoint(struct target *target,
                break_insn = NDS32_BREAK_32;
        }
 
-       if (breakpoint->orig_instr != NULL)
-               free(breakpoint->orig_instr);
+       free(breakpoint->orig_instr);
 
        breakpoint->orig_instr = malloc(breakpoint->length);
        memcpy(breakpoint->orig_instr, &data, breakpoint->length);
@@ -2334,10 +2333,8 @@ int nds32_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fil
        LOG_DEBUG("hit syscall ID: 0x%" PRIx32, syscall_id);
 
        /* free previous identifier storage */
-       if (NULL != fileio_info->identifier) {
-               free(fileio_info->identifier);
-               fileio_info->identifier = NULL;
-       }
+       free(fileio_info->identifier);
+       fileio_info->identifier = NULL;
 
        uint32_t reg_r0, reg_r1, reg_r2;
        nds32_get_mapped_reg(nds32, R0, &reg_r0);
index f28815d..1d05944 100644 (file)
@@ -190,12 +190,8 @@ static int jsp_connection_closed(struct connection *connection)
        if (ERROR_OK != retval)
                return retval;
 
-       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 93fb9c6..d685359 100644 (file)
@@ -943,8 +943,7 @@ static int or1k_add_breakpoint(struct target *target,
                return retval;
        }
 
-       if (breakpoint->orig_instr != NULL)
-               free(breakpoint->orig_instr);
+       free(breakpoint->orig_instr);
 
        breakpoint->orig_instr = malloc(breakpoint->length);
        memcpy(breakpoint->orig_instr, &data, breakpoint->length);
index afc773f..31b2487 100644 (file)
@@ -975,8 +975,7 @@ static int or1k_adv_jtag_write_memory(struct or1k_jtag *jtag_info,
                                             size, blocks_this_round,
                                             block_count_address);
                if (retval != ERROR_OK) {
-                       if (t != NULL)
-                               free(t);
+                       free(t);
                        return retval;
                }
 
@@ -985,9 +984,7 @@ static int or1k_adv_jtag_write_memory(struct or1k_jtag *jtag_info,
                block_count_buffer += size * MAX_BURST_SIZE;
        }
 
-       if (t != NULL)
-               free(t);
-
+       free(t);
        return ERROR_OK;
 }
 
index afd0cc8..de2f095 100644 (file)
@@ -276,8 +276,7 @@ static void riscv_free_registers(struct target *target)
        /* Free the shared structure use for most registers. */
        if (target->reg_cache) {
                if (target->reg_cache->reg_list) {
-                       if (target->reg_cache->reg_list[0].arch_info)
-                               free(target->reg_cache->reg_list[0].arch_info);
+                       free(target->reg_cache->reg_list[0].arch_info);
                        /* Free the ones we allocated separately. */
                        for (unsigned i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
                                free(target->reg_cache->reg_list[i].arch_info);
@@ -1558,8 +1557,7 @@ int parse_ranges(range_t **ranges, const char **argv)
                }
 
                if (pass == 0) {
-                       if (*ranges)
-                               free(*ranges);
+                       free(*ranges);
                        *ranges = calloc(range + 2, sizeof(range_t));
                } else {
                        (*ranges)[range].low = 1;
@@ -2487,8 +2485,7 @@ int riscv_init_registers(struct target *target)
                calloc(target->reg_cache->num_regs, sizeof(struct reg));
 
        const unsigned int max_reg_name_len = 12;
-       if (info->reg_names)
-               free(info->reg_names);
+       free(info->reg_names);
        info->reg_names =
                calloc(target->reg_cache->num_regs, max_reg_name_len);
        char *reg_name = info->reg_names;
index a918b3a..fa609ef 100644 (file)
@@ -1788,10 +1788,8 @@ static void target_split_working_area(struct working_area *area, uint32_t size)
 
                /* If backup memory was allocated to this area, it has the wrong size
                 * now so free it and it will be reallocated if/when needed */
-               if (area->backup) {
-                       free(area->backup);
-                       area->backup = NULL;
-               }
+               free(area->backup);
+               area->backup = NULL;
        }
 }
 
@@ -1811,16 +1809,13 @@ static void target_merge_working_areas(struct target *target)
                        /* Remove the last */
                        struct working_area *to_be_freed = c->next;
                        c->next = c->next->next;
-                       if (to_be_freed->backup)
-                               free(to_be_freed->backup);
+                       free(to_be_freed->backup);
                        free(to_be_freed);
 
                        /* If backup memory was allocated to the remaining area, it's has
                         * the wrong size now */
-                       if (c->backup) {
-                               free(c->backup);
-                               c->backup = NULL;
-                       }
+                       free(c->backup);
+                       c->backup = NULL;
                } else {
                        c = c->next;
                }
@@ -2050,8 +2045,7 @@ static void target_destroy(struct target *target)
        if (target->type->deinit_target)
                target->type->deinit_target(target);
 
-       if (target->semihosting)
-               free(target->semihosting);
+       free(target->semihosting);
 
        jtag_unregister_event_callback(jtag_enable_callback, target);
 
@@ -5833,11 +5827,8 @@ static struct FastLoad *fastload;
 static void free_fastload(void)
 {
        if (fastload != NULL) {
-               int i;
-               for (i = 0; i < fastload_num; i++) {
-                       if (fastload[i].data)
-                               free(fastload[i].data);
-               }
+               for (int i = 0; i < fastload_num; i++)
+                       free(fastload[i].data);
                free(fastload);
                fastload = NULL;
        }
index 943bf1f..f2ceb03 100644 (file)
@@ -62,10 +62,9 @@ COMMAND_HANDLER(handle_trace_point_command)
        }
 
        if (!strcmp(CMD_ARGV[0], "clear")) {
-               if (trace->trace_points) {
-                       free(trace->trace_points);
-                       trace->trace_points = NULL;
-               }
+               free(trace->trace_points);
+               trace->trace_points = NULL;
+
                trace->num_trace_points = 0;
                trace->trace_points_size = 0;
 
@@ -102,8 +101,7 @@ COMMAND_HANDLER(handle_trace_history_command)
                        return ERROR_OK;
                }
 
-               if (trace->trace_history)
-                       free(trace->trace_history);
+               free(trace->trace_history);
 
                COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
                trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
index 770b325..6d1d426 100644 (file)
@@ -1110,8 +1110,7 @@ static void xscale_free_trace_data(struct xscale_common *xscale)
        struct xscale_trace_data *td = xscale->trace.data;
        while (td) {
                struct xscale_trace_data *next_td = td->next;
-               if (td->entries)
-                       free(td->entries);
+               free(td->entries);
                free(td);
                td = next_td;
        }