NOR/CFI: fix memory leak; check malloc return value
[openocd.git] / src / flash / nor / cfi.c
index 72c95dd1ab32c30bfef8c2189e4636221def016a..8c1aacad263798e11179fb8790ccb276f731ed15 100644 (file)
@@ -355,8 +355,17 @@ static int cfi_read_intel_pri_ext(struct flash_bank *bank)
 {
        int retval;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
-       struct cfi_intel_pri_ext *pri_ext = malloc(sizeof(struct cfi_intel_pri_ext));
+       struct cfi_intel_pri_ext *pri_ext;
 
+       if (cfi_info->pri_ext)
+               free(cfi_info->pri_ext);
+
+       pri_ext = malloc(sizeof(struct cfi_intel_pri_ext));
+       if (pri_ext == NULL)
+       {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
        cfi_info->pri_ext = pri_ext;
 
        pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
@@ -413,8 +422,17 @@ static int cfi_read_spansion_pri_ext(struct flash_bank *bank)
 {
        int retval;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
-       struct cfi_spansion_pri_ext *pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
+       struct cfi_spansion_pri_ext *pri_ext;
 
+       if (cfi_info->pri_ext)
+               free(cfi_info->pri_ext);
+
+       pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
+       if (pri_ext == NULL)
+       {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
        cfi_info->pri_ext = pri_ext;
 
        pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
@@ -476,7 +494,17 @@ static int cfi_read_atmel_pri_ext(struct flash_bank *bank)
        int retval;
        struct cfi_atmel_pri_ext atmel_pri_ext;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
-       struct cfi_spansion_pri_ext *pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
+       struct cfi_spansion_pri_ext *pri_ext;
+
+       if (cfi_info->pri_ext)
+               free(cfi_info->pri_ext);
+
+       pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
+       if (pri_ext == NULL)
+       {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
 
        /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
         * but a different primary extended query table.
@@ -644,6 +672,8 @@ FLASH_BANK_COMMAND_HANDLER(cfi_flash_bank_command)
 
        cfi_info = malloc(sizeof(struct cfi_flash_bank));
        cfi_info->probed = 0;
+       cfi_info->erase_region_info = 0;
+       cfi_info->pri_ext = NULL;
        bank->driver_priv = cfi_info;
 
        cfi_info->write_algorithm = NULL;
@@ -805,8 +835,6 @@ static int cfi_intel_protect(struct flash_bank *bank, int set, int first, int la
        int retval;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
        struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
-       struct target *target = bank->target; /* FIXME: to be removed */
-       uint8_t command[CFI_MAX_BUS_WIDTH]; /* FIXME: to be removed */
        int retry = 0;
        int i;
 
@@ -820,16 +848,12 @@ static int cfi_intel_protect(struct flash_bank *bank, int set, int first, int la
 
        for (i = first; i <= last; i++)
        {
-               cfi_command(bank, 0x60, command); /* FIXME: to be removed */
-               LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32, flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
                if ((retval = cfi_send_command(bank, 0x60, flash_address(bank, i, 0x0))) != ERROR_OK)
                {
                        return retval;
                }
                if (set)
                {
-                       cfi_command(bank, 0x01, command); /* FIXME: to be removed */
-                       LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32 , flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
                        if ((retval = cfi_send_command(bank, 0x01, flash_address(bank, i, 0x0))) != ERROR_OK)
                        {
                                return retval;
@@ -838,8 +862,6 @@ static int cfi_intel_protect(struct flash_bank *bank, int set, int first, int la
                }
                else
                {
-                       cfi_command(bank, 0xd0, command); /* FIXME: to be removed */
-                       LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32, flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
                        if ((retval = cfi_send_command(bank, 0xd0, flash_address(bank, i, 0x0))) != ERROR_OK)
                        {
                                return retval;
@@ -1434,6 +1456,11 @@ static int cfi_spansion_write_block(struct flash_bank *bank, uint8_t *buffer, ui
 
                /* convert bus-width dependent algorithm code to correct endiannes */
                target_code = malloc(target_code_size);
+               if (target_code == NULL)
+               {
+                       LOG_ERROR("Out of memory");
+                       return ERROR_FAIL;
+               }
                cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
 
                /* allocate working area */
@@ -2107,6 +2134,16 @@ static int cfi_probe(struct flash_bank *bank)
        }
 
        cfi_info->probed = 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;
+       }
 
        /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
         * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)