This is an automated email from Gerrit.

"Antonio Borneo <[email protected]>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9384

-- gerrit

commit 325d95047411cdaf89003bd87fb2d56382312014
Author: Antonio Borneo <[email protected]>
Date:   Thu Dec 25 11:31:35 2025 +0100

    flash: nor: improve check on memory allocations
    
    Add check for failed allocation.
    Move allocation and check before changing flash information.
    
    Change-Id: I5b2ab6bc12ea15a5d8f634ed00cf0a0bc7e5a517
    Signed-off-by: Antonio Borneo <[email protected]>

diff --git a/src/flash/nor/core.c b/src/flash/nor/core.c
index 5c4f2accaf..d0c803533b 100644
--- a/src/flash/nor/core.c
+++ b/src/flash/nor/core.c
@@ -343,6 +343,10 @@ static int default_flash_mem_blank_check(struct flash_bank 
*bank)
        }
 
        uint8_t *buffer = malloc(buffer_size);
+       if (!buffer) {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
 
        for (unsigned int i = 0; i < bank->num_sectors; i++) {
                uint32_t j;
@@ -389,8 +393,10 @@ int default_flash_blank_check(struct flash_bank *bank)
 
        struct target_memory_check_block *block_array;
        block_array = malloc(bank->num_sectors * sizeof(struct 
target_memory_check_block));
-       if (!block_array)
-               return default_flash_mem_blank_check(bank);
+       if (!block_array) {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
 
        for (unsigned int i = 0; i < bank->num_sectors; i++) {
                block_array[i].address = bank->base + bank->sectors[i].offset;
@@ -735,11 +741,25 @@ int flash_write_unlock_verify(struct target *target, 
struct image *image,
        unsigned int section;
        uint32_t section_offset;
        struct flash_bank *c;
-       int *padding;
 
        section = 0;
        section_offset = 0;
 
+       /* allocate padding array */
+       int *padding = calloc(image->num_sections, sizeof(*padding));
+
+       /* This fn requires all sections to be in ascending order of addresses,
+        * whereas an image can have sections out of order. */
+       struct imagesection **sections = malloc(sizeof(struct imagesection *) *
+                       image->num_sections);
+
+       if (!padding || !sections) {
+               LOG_ERROR("Out of memory");
+               free(padding);
+               free(sections);
+               return ERROR_FAIL;
+       }
+
        if (written)
                *written = 0;
 
@@ -750,14 +770,6 @@ int flash_write_unlock_verify(struct target *target, 
struct image *image,
                flash_set_dirty();
        }
 
-       /* allocate padding array */
-       padding = calloc(image->num_sections, sizeof(*padding));
-
-       /* This fn requires all sections to be in ascending order of addresses,
-        * whereas an image can have sections out of order. */
-       struct imagesection **sections = malloc(sizeof(struct imagesection *) *
-                       image->num_sections);
-
        for (unsigned int i = 0; i < image->num_sections; i++)
                sections[i] = &image->sections[i];
 

-- 

Reply via email to