diff options
author | Tomas Vanek <vanekt@fbl.cz> | 2023-09-05 18:32:57 +0200 |
---|---|---|
committer | Antonio Borneo <borneo.antonio@gmail.com> | 2023-09-23 14:30:20 +0000 |
commit | 040757b7e6d7bc4951af3d683f774f0660d002b5 (patch) | |
tree | 995781c52523c4f7f24c036af2f6a66f5f75b4c0 | |
parent | 870769b0ba9f4dae6ada9d8b1a40d75bd83aaa06 (diff) | |
download | riscv-openocd-040757b7e6d7bc4951af3d683f774f0660d002b5.zip riscv-openocd-040757b7e6d7bc4951af3d683f774f0660d002b5.tar.gz riscv-openocd-040757b7e6d7bc4951af3d683f774f0660d002b5.tar.bz2 |
flash/nor/at91sam7: fix flash bank allocation
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 <vanekt@fbl.cz>
Change-Id: Id890496bfbadb7970ef583256aa4f30a7bff832f
Reviewed-on: https://review.openocd.org/c/openocd/+/7884
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
-rw-r--r-- | src/flash/nor/at91sam7.c | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/flash/nor/at91sam7.c b/src/flash/nor/at91sam7.c index 8d8cf22..8bb2994 100644 --- a/src/flash/nor/at91sam7.c +++ b/src/flash/nor/at91sam7.c @@ -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 */ @@ -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 */ |