flash: jtagspi: fix clang build warning 18/7718/2
authorAntonio Borneo <borneo.antonio@gmail.com>
Sat, 27 May 2023 15:01:58 +0000 (17:01 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Fri, 2 Jun 2023 20:58:53 +0000 (20:58 +0000)
Clang is unable to fully track the content of the array
write_buffer[] and incorrectly complains that it could contain
some uninitialized value.

To help clang to track the execution flow, rewrite the handling of
the buffer by using simpler indexing and by moving away cmd_byte
from the first buffer's element to the variable cmd_byte.

While there:
- fix the error codes returned while parsing the command line and
- use directly command_print_sameline() instead of passing through
  intermediate buffers.

Change-Id: I1969e896887ea3a4abebee057cc04c03005fa57c
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7718
Tested-by: jenkins
src/flash/nor/jtagspi.c

index 866548a7e0148b44c71d5d5e0622145b37a6b6d3..6bb3af9b7df1a89ff4d60a4cbb0649631f7dc2a2 100644 (file)
@@ -301,24 +301,18 @@ COMMAND_HANDLER(jtagspi_handle_set)
 COMMAND_HANDLER(jtagspi_handle_cmd)
 {
        struct flash_bank *bank;
-       unsigned int index = 1;
-       const int max = 21;
-       uint8_t num_write, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
-       uint8_t data, *ptr;
-       char temp[4], output[(2 + max + (1 << CHAR_BIT)) * 3 + 8];
-       int retval;
+       const unsigned int max = 20;
+       uint8_t cmd_byte, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
 
        LOG_DEBUG("%s", __func__);
 
-       if (CMD_ARGC < 3) {
-               command_print(CMD, "jtagspi: not enough arguments");
+       if (CMD_ARGC < 3)
                return ERROR_COMMAND_SYNTAX_ERROR;
-       }
 
-       num_write = CMD_ARGC - 2;
+       uint8_t num_write = CMD_ARGC - 3;
        if (num_write > max) {
-               LOG_ERROR("at most %d bytes may be send", max);
-               return ERROR_COMMAND_SYNTAX_ERROR;
+               command_print(CMD, "at most %d bytes may be send", max);
+               return ERROR_COMMAND_ARGUMENT_INVALID;
        }
 
        /* calling flash_command_get_bank without probing because we like to be
@@ -326,33 +320,31 @@ COMMAND_HANDLER(jtagspi_handle_cmd)
           "release from power down" is needed before probing when flash is in
           power down mode.
         */
-       retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
+       int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
                                                                &bank, false);
-       if (ERROR_OK != retval)
+       if (retval != ERROR_OK)
                return retval;
 
-       COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], num_read);
+       COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], num_read);
+       COMMAND_PARSE_NUMBER(u8, CMD_ARGV[2], cmd_byte);
 
-       snprintf(output, sizeof(output), "spi: ");
-       for (ptr = &write_buffer[0] ; index < CMD_ARGC; index++) {
-               COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index], data);
-               *ptr++ = data;
-               snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
-               strncat(output, temp, sizeof(output) - strlen(output) - 1);
-       }
-       strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
+       for (unsigned int i = 0; i < num_write; i++)
+               COMMAND_PARSE_NUMBER(u8, CMD_ARGV[i + 3], write_buffer[i]);
 
        /* process command */
-       ptr = &read_buffer[0];
-       retval = jtagspi_cmd(bank, write_buffer[0], &write_buffer[1], num_write - 1, ptr, -num_read);
+       retval = jtagspi_cmd(bank, cmd_byte, write_buffer, num_write, read_buffer, -num_read);
        if (retval != ERROR_OK)
                return retval;
 
-       for ( ; num_read > 0; num_read--) {
-               snprintf(temp, sizeof(temp), "%02" PRIx8 " ", *ptr++);
-               strncat(output, temp, sizeof(output) - strlen(output) - 1);
-       }
-       command_print(CMD, "%s", output);
+       command_print_sameline(CMD, "spi: %02" PRIx8, cmd_byte);
+
+       for (unsigned int i = 0; i < num_write; i++)
+               command_print_sameline(CMD, " %02" PRIx8, write_buffer[i]);
+
+       command_print_sameline(CMD, " ->");
+
+       for (unsigned int i = 0; i < num_read; i++)
+               command_print_sameline(CMD, " %02" PRIx8, read_buffer[i]);
 
        return ERROR_OK;
 }

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)