]> www.infradead.org Git - users/borneoa/openocd-next.git/commitdiff
target: use array size to constraint the loop
authorAntonio Borneo <borneo.antonio@gmail.com>
Sat, 14 Jun 2025 13:02:04 +0000 (15:02 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sun, 29 Jun 2025 07:32:25 +0000 (07:32 +0000)
Instead of using NULL terminated arrays to determine the last
element of the array, use the size of the array.

Change-Id: I3cdc0f6aef8a5110073aeef333c439e61fc54032
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8952
Tested-by: jenkins
Reviewed-by: Brandon Martin
src/target/target.c

index 8bf654a272a9b68f626b711a0d447f3406799173..995adbc9d3131a56a3fc179ecb6425b6f7f7ac58 100644 (file)
@@ -110,7 +110,6 @@ static struct target_type *target_types[] = {
        &testee_target,
        &xscale_target,
        &xtensa_chip_target,
-       NULL,
 };
 
 struct target *all_targets;
@@ -5708,7 +5707,6 @@ static const struct command_registration target_instance_command_handlers[] = {
 COMMAND_HANDLER(handle_target_create)
 {
        int retval = ERROR_OK;
-       int x;
 
        if (CMD_ARGC < 2)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -5732,15 +5730,16 @@ COMMAND_HANDLER(handle_target_create)
                LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
        }
        /* now does target type exist */
-       for (x = 0 ; target_types[x] ; x++) {
+       size_t x;
+       for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
                if (strcmp(cp, target_types[x]->name) == 0) {
                        /* found */
                        break;
                }
        }
-       if (!target_types[x]) {
+       if (x == ARRAY_SIZE(target_types)) {
                char *all = NULL;
-               for (x = 0 ; target_types[x] ; x++) {
+               for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
                        char *prev = all;
                        if (all)
                                all = alloc_printf("%s, %s", all, target_types[x]->name);
@@ -5942,7 +5941,7 @@ COMMAND_HANDLER(handle_target_types)
        if (CMD_ARGC != 0)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       for (unsigned int x = 0; target_types[x]; x++)
+       for (size_t x = 0; x < ARRAY_SIZE(target_types); x++)
                command_print(CMD, "%s", target_types[x]->name);
 
        return ERROR_OK;