src/flash/nor/virtual: handle null pointers 82/5782/5
authorChristopher Head <chead@zaber.com>
Fri, 17 Jul 2020 21:44:35 +0000 (14:44 -0700)
committerTomas Vanek <vanekt@fbl.cz>
Sun, 9 Aug 2020 14:30:23 +0000 (15:30 +0100)
Either of protect and protect_check driver callbacks can be NULL. In
virtual_protect, reuse flash_driver_protect which checks for that case
and generates a nice error message and return code. In
virtual_protect_check, there is no corresponding flash_driver_*
function, so add the NULL check directly.

Change-Id: Ia63d85ede640a8382cf5cad0760f5d1ffe4d7cfe
Signed-off-by: Christopher Head <chead@zaber.com>
Reviewed-on: http://openocd.zylin.com/5782
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
src/flash/nor/virtual.c

index c9e1942..1aa12fe 100644 (file)
@@ -79,33 +79,25 @@ static int virtual_protect(struct flash_bank *bank, int set, unsigned int first,
                unsigned int last)
 {
        struct flash_bank *master_bank = virtual_get_master_bank(bank);
-       int retval;
 
        if (master_bank == NULL)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       /* call master handler */
-       retval = master_bank->driver->protect(master_bank, set, first, last);
-       if (retval != ERROR_OK)
-               return retval;
-
-       return ERROR_OK;
+       return flash_driver_protect(master_bank, set, first, last);
 }
 
 static int virtual_protect_check(struct flash_bank *bank)
 {
        struct flash_bank *master_bank = virtual_get_master_bank(bank);
-       int retval;
 
        if (master_bank == NULL)
                return ERROR_FLASH_OPERATION_FAILED;
 
-       /* call master handler */
-       retval = master_bank->driver->protect_check(master_bank);
-       if (retval != ERROR_OK)
-               return retval;
+       if (master_bank->driver->protect_check == NULL)
+               return ERROR_FLASH_OPER_UNSUPPORTED;
 
-       return ERROR_OK;
+       /* call master handler */
+       return master_bank->driver->protect_check(master_bank);
 }
 
 static int virtual_erase(struct flash_bank *bank, unsigned int first,