This is an automated email from Gerrit.

"Tomas Vanek <van...@fbl.cz>" just uploaded a new patch set to Gerrit, which 
you can find at https://review.openocd.org/c/openocd/+/7884

-- gerrit

commit 57aaadfb2deec2075f37d020e61853e19ef35f5f
Author: Tomas Vanek <van...@fbl.cz>
Date:   Tue Sep 5 18:32:57 2023 +0200

    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 <van...@fbl.cz>
    Change-Id: Id890496bfbadb7970ef583256aa4f30a7bff832f

diff --git a/src/flash/nor/at91sam7.c b/src/flash/nor/at91sam7.c
index 8d8cf22268..8bb2994ed1 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 */

-- 

Reply via email to