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

There are cases where the pointer is set to NULL after free(), but
then re-assigned within few lines. Drop the setting to NULL when
this is evident. Anyway, the compiler will remove the useless
assignment so no reason to be too much aggressive in this change.

Change-Id: I55b2ce7cbe201410016398933e34d33a4b66e30b
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/5811
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2020-08-17 10:08:35 +02:00
parent 47d29ebe11
commit 62329444ab
30 changed files with 71 additions and 161 deletions

View File

@ -99,14 +99,11 @@ int nand_fileio_cleanup(struct nand_fileio_state *state)
if (state->file_opened)
fileio_close(state->fileio);
if (state->oob) {
free(state->oob);
state->oob = NULL;
}
if (state->page) {
free(state->page);
state->page = NULL;
}
free(state->oob);
state->oob = NULL;
free(state->page);
state->page = NULL;
return ERROR_OK;
}
int nand_fileio_finish(struct nand_fileio_state *state)

View File

@ -672,10 +672,7 @@ static int ambiqmicro_probe(struct flash_bank *bank)
if (retval != ERROR_OK)
return retval;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
/* provide this for the benefit of the NOR flash framework */
bank->size = ambiqmicro_info->pagesize * ambiqmicro_info->num_pages;

View File

@ -3530,8 +3530,7 @@ static int sam3_write(struct flash_bank *bank,
LOG_DEBUG("Done!");
r = ERROR_OK;
done:
if (pagebuffer)
free(pagebuffer);
free(pagebuffer);
return r;
}

View File

@ -3009,8 +3009,7 @@ static int sam4_write(struct flash_bank *bank,
LOG_DEBUG("Done!");
r = ERROR_OK;
done:
if (pagebuffer)
free(pagebuffer);
free(pagebuffer);
return r;
}

View File

@ -915,9 +915,7 @@ static int samd_write(struct flash_bank *bank, const uint8_t *buffer,
}
free_pb:
if (pb)
free(pb);
free(pb);
return res;
}

View File

@ -731,9 +731,7 @@ static int same5_write(struct flash_bank *bank, const uint8_t *buffer,
}
free_pb:
if (pb)
free(pb);
free(pb);
return res;
}

View File

@ -333,10 +333,7 @@ static int avrf_probe(struct flash_bank *bank)
}
if (avr_info != NULL) {
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
/* chip found */
bank->base = 0x00000000;

View File

@ -438,10 +438,7 @@ static int cc3220sf_probe(struct flash_bank *bank)
size = FLASH_NUM_SECTORS * FLASH_SECTOR_SIZE;
num_sectors = FLASH_NUM_SECTORS;
if (NULL != bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
if (NULL == bank->sectors)

View File

@ -423,8 +423,7 @@ static int cfi_read_intel_pri_ext(struct flash_bank *bank)
struct cfi_flash_bank *cfi_info = bank->driver_priv;
struct cfi_intel_pri_ext *pri_ext;
if (cfi_info->pri_ext)
free(cfi_info->pri_ext);
free(cfi_info->pri_ext);
pri_ext = malloc(sizeof(struct cfi_intel_pri_ext));
if (pri_ext == NULL) {
@ -520,8 +519,7 @@ static int cfi_read_spansion_pri_ext(struct flash_bank *bank)
struct cfi_flash_bank *cfi_info = bank->driver_priv;
struct cfi_spansion_pri_ext *pri_ext;
if (cfi_info->pri_ext)
free(cfi_info->pri_ext);
free(cfi_info->pri_ext);
pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
if (pri_ext == NULL) {
@ -623,8 +621,7 @@ static int cfi_read_atmel_pri_ext(struct flash_bank *bank)
struct cfi_flash_bank *cfi_info = bank->driver_priv;
struct cfi_spansion_pri_ext *pri_ext;
if (cfi_info->pri_ext)
free(cfi_info->pri_ext);
free(cfi_info->pri_ext);
pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
if (pri_ext == NULL) {
@ -2593,14 +2590,12 @@ int cfi_probe(struct flash_bank *bank)
cfi_info->probed = false;
cfi_info->num_erase_regions = 0;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
if (cfi_info->erase_region_info) {
free(cfi_info->erase_region_info);
cfi_info->erase_region_info = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
free(cfi_info->erase_region_info);
cfi_info->erase_region_info = NULL;
/* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
* while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa

View File

@ -951,9 +951,7 @@ reset_pg_and_lock:
retval = retval2;
cleanup:
if (new_buffer)
free(new_buffer);
free(new_buffer);
return retval;
}
@ -987,10 +985,8 @@ static int efm32x_probe(struct flash_bank *bank)
assert(num_pages > 0);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
bank->base = base_address;
bank->size = (num_pages * efm32_mcu_info.page_size);

View File

@ -724,10 +724,7 @@ static int em357_probe(struct flash_bank *bank)
LOG_INFO("flash size = %dkbytes", num_pages*page_size/1024);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->base = base_address;
bank->size = (num_pages * page_size);

View File

@ -2736,14 +2736,11 @@ static int kinetis_probe(struct flash_bank *bank)
" please report to OpenOCD mailing list", fcfg2_maxaddr1);
}
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
if (bank->prot_blocks) {
free(bank->prot_blocks);
bank->prot_blocks = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
free(bank->prot_blocks);
bank->prot_blocks = NULL;
if (k_bank->sector_size == 0) {
LOG_ERROR("Unknown sector size for bank %u", bank->bank_number);

View File

@ -1145,10 +1145,7 @@ static int kinetis_ke_probe(struct flash_bank *bank)
break;
}
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
assert(bank->num_sectors > 0);
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);

View File

@ -651,10 +651,7 @@ static int max32xxx_probe(struct flash_bank *bank)
uint32_t arm_id[2];
uint16_t arm_pid;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
/* provide this for the benefit of the NOR flash framework */
bank->size = info->flash_size;

View File

@ -458,8 +458,7 @@ reset_pg_and_lock:
retval = retval2;
free_buffer:
if (new_buffer)
free(new_buffer);
free(new_buffer);
/* read some bytes bytes to flush buffer in flash accelerator.
* See errata for 1986VE1T and 1986VE3. Error 0007 */
@ -573,10 +572,7 @@ static int mdr_probe(struct flash_bank *bank)
page_count = mdr_info->page_count;
page_size = bank->size / page_count;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->num_sectors = page_count;
bank->sectors = malloc(sizeof(struct flash_sector) * page_count);

View File

@ -902,10 +902,8 @@ static int msp432_probe(struct flash_bank *bank)
}
}
if (NULL != bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
if (num_sectors > 0) {
bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
@ -1046,7 +1044,7 @@ static void msp432_flash_free_driver_priv(struct flash_bank *bank)
/* A single private struct is shared between main and info banks */
/* Only free it on the call for main bank */
if (is_main && (NULL != bank->driver_priv))
if (is_main)
free(bank->driver_priv);
/* Forget about the private struct on both main and info banks */

View File

@ -1469,9 +1469,7 @@ static int niietcm4_write(struct flash_bank *bank, const uint8_t *buffer,
}
free_buffer:
if (new_buffer)
free(new_buffer);
free(new_buffer);
return retval;
}
@ -1681,10 +1679,9 @@ static int niietcm4_probe(struct flash_bank *bank)
struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
struct target *target = bank->target;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
uint32_t retval;
uint32_t chipid;

View File

@ -591,8 +591,7 @@ static int pic32mx_write_block(struct flash_bank *bank, const uint8_t *buffer,
destroy_reg_param(&reg_params[1]);
destroy_reg_param(&reg_params[2]);
if (new_buffer != NULL)
free(new_buffer);
free(new_buffer);
return retval;
}
@ -774,10 +773,7 @@ static int pic32mx_probe(struct flash_bank *bank)
LOG_INFO("flash size = %" PRId32 "kbytes", num_pages / 1024);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
/* calculate numbers of pages */
num_pages /= page_size;

View File

@ -610,8 +610,7 @@ static int psoc4_protect(struct flash_bank *bank, int set, unsigned int first,
break;
}
if (sysrq_buffer)
free(sysrq_buffer);
free(sysrq_buffer);
psoc4_protect_check(bank);
return retval;
@ -714,9 +713,7 @@ static int psoc4_write(struct flash_bank *bank, const uint8_t *buffer,
cleanup:
jtag_poll_set_enabled(save_poll);
if (sysrq_buffer)
free(sysrq_buffer);
free(sysrq_buffer);
return retval;
}
@ -827,9 +824,7 @@ static int psoc4_probe(struct flash_bank *bank)
}
}
if (bank->sectors) {
free(bank->sectors);
}
free(bank->sectors);
psoc4_info->family_id = family_id;
psoc4_info->num_macros = num_macros;

View File

@ -585,10 +585,8 @@ static int psoc6_probe(struct flash_bank *bank)
/* Calculate size of Main Flash*/
uint32_t flash_sz_bytes = bank_cnt * row_cnt * row_sz;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
size_t bank_size = 0;

View File

@ -801,10 +801,7 @@ static int sim3x_probe(struct flash_bank *bank)
if (ret != ERROR_OK)
return ret;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->base = FLASH_BASE_ADDRESS;
bank->size = sim3x_info->flash_size_kb * SIM3X_FLASH_PAGE_SIZE;

View File

@ -1250,10 +1250,7 @@ static int stellaris_probe(struct flash_bank *bank)
if (retval != ERROR_OK)
return retval;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
/* provide this for the benefit of the NOR flash framework */
bank->size = stellaris_info->num_pages * stellaris_info->pagesize;

View File

@ -617,9 +617,7 @@ reset_pg_and_lock:
retval = retval2;
cleanup:
if (new_buffer)
free(new_buffer);
free(new_buffer);
return retval;
}
@ -872,15 +870,11 @@ static int stm32x_probe(struct flash_bank *bank)
/* check that calculation result makes sense */
assert(num_pages > 0);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
if (bank->prot_blocks) {
free(bank->prot_blocks);
bank->prot_blocks = NULL;
}
free(bank->prot_blocks);
bank->prot_blocks = NULL;
bank->base = base_address;
bank->size = (num_pages * page_size);

View File

@ -1013,17 +1013,13 @@ static int stm32x_probe(struct flash_bank *bank)
stm32x_info->protection_bits = 12; /* max. number of nWRPi bits (in FLASH_OPTCR !!!) */
num_prot_blocks = 0;
if (bank->sectors) {
free(bank->sectors);
bank->num_sectors = 0;
bank->sectors = NULL;
}
free(bank->sectors);
bank->num_sectors = 0;
bank->sectors = NULL;
if (bank->prot_blocks) {
free(bank->prot_blocks);
bank->num_prot_blocks = 0;
bank->prot_blocks = NULL;
}
free(bank->prot_blocks);
bank->num_prot_blocks = 0;
bank->prot_blocks = NULL;
/* if explicitly called out as OTP bank, short circuit probe */
if (stm32x_is_otp(bank)) {

View File

@ -853,8 +853,7 @@ static int stm32x_probe(struct flash_bank *bank)
bank->num_sectors = flash_size_in_kb / stm32x_info->part_info->page_size_kb;
assert(bank->num_sectors > 0);
if (bank->sectors)
free(bank->sectors);
free(bank->sectors);
bank->sectors = alloc_block_array(0, stm32x_info->part_info->page_size_kb * 1024,
bank->num_sectors);
@ -871,8 +870,7 @@ static int stm32x_probe(struct flash_bank *bank)
bank->num_prot_blocks = bank->num_sectors / wpsn;
assert(bank->num_prot_blocks > 0);
if (bank->prot_blocks)
free(bank->prot_blocks);
free(bank->prot_blocks);
bank->prot_blocks = alloc_block_array(0, stm32x_info->part_info->page_size_kb * wpsn * 1024,
bank->num_prot_blocks);

View File

@ -1040,10 +1040,7 @@ static int stm32l4_probe(struct flash_bank *bank)
assert((stm32l4_info->wrpxxr_mask & 0xFFFF0000) == 0);
LOG_DEBUG("WRPxxR mask 0x%04" PRIx16, (uint16_t)stm32l4_info->wrpxxr_mask);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->size = (flash_size_kb + gap_size_kb) * 1024;
bank->base = STM32_FLASH_BANK_BASE;

View File

@ -836,10 +836,7 @@ static int stm32lx_probe(struct flash_bank *bank)
/* calculate numbers of sectors (4kB per sector) */
unsigned int num_sectors = (flash_size_in_kb * 1024) / FLASH_SECTOR_SIZE;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->size = flash_size_in_kb * 1024;
bank->base = base_address;

View File

@ -156,10 +156,7 @@ COMMAND_HANDLER(swm050_handle_mass_erase_command)
FLASH_BANK_COMMAND_HANDLER(swm050_flash_bank_command)
{
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->write_start_alignment = 4;
bank->write_end_alignment = 4;
bank->size = SWM050_FLASH_PAGE_SIZE * SWM050_FLASH_PAGES;

View File

@ -148,11 +148,9 @@ static int tms470_read_part_info(struct flash_bank *bank)
rom_flash = (device_ident_reg >> 10) & 1;
part_number = (device_ident_reg >> 3) & 0x7f;
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
bank->num_sectors = 0;
}
free(bank->sectors);
bank->sectors = NULL;
bank->num_sectors = 0;
/*
* If the part number is known, determine if the flash bank is valid

View File

@ -331,10 +331,8 @@ static int w600_probe(struct flash_bank *bank)
/* check that calculation result makes sense */
assert(num_pages > 0);
if (bank->sectors) {
free(bank->sectors);
bank->sectors = NULL;
}
free(bank->sectors);
bank->sectors = NULL;
bank->base = W600_FLASH_BASE;
bank->size = num_pages * W600_FLASH_SECSIZE;