Skip to content

Commit

Permalink
flash/nor/at91sam7: fix flash bank allocation
Browse files Browse the repository at this point in the history
at91sam7 flash driver allocates a flash bank based on detected flash
structure.
Use calloc() instead of malloc() - struct flash_bank has to be zeroed.

While on this:
Return error in case of struct flash_bank or driver_priv allocation fail.
Set default_padded_value and erased_value.
Use strdup() on bank->name, pointer is freed in flash_free_all_banks()

Signed-off-by: Tomas Vanek <[email protected]>
Change-Id: Id890496bfbadb7970ef583256aa4f30a7bff832f
Reviewed-on: https://review.openocd.org/c/openocd/+/7884
Tested-by: jenkins
Reviewed-by: Antonio Borneo <[email protected]>
  • Loading branch information
tom-van authored and borneoa committed Sep 23, 2023
1 parent 870769b commit 040757b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/flash/nor/at91sam7.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,22 @@ static int at91sam7_read_part_info(struct flash_bank *bank)
if (bnk > 0) {
if (!t_bank->next) {
/* create a new flash bank element */
struct flash_bank *fb = malloc(sizeof(struct flash_bank));
struct flash_bank *fb = calloc(sizeof(struct flash_bank), 1);
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
fb->name = "sam7_probed";
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}
fb->name = strdup("sam7_probed");
fb->next = NULL;

/* link created bank in 'flash_banks' list */
Expand Down Expand Up @@ -738,11 +749,22 @@ FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
if (bnk > 0) {
if (!t_bank->next) {
/* create a new bank element */
struct flash_bank *fb = malloc(sizeof(struct flash_bank));
struct flash_bank *fb = calloc(sizeof(struct flash_bank), 1);
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
fb->name = "sam7_probed";
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}
fb->name = strdup("sam7_probed");
fb->next = NULL;

/* link created bank in 'flash_banks' list */
Expand Down

0 comments on commit 040757b

Please sign in to comment.