Simplify the handle_md_command routine in target.c:
authorzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Sun, 31 May 2009 06:00:28 +0000 (06:00 +0000)
committerzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Sun, 31 May 2009 06:00:28 +0000 (06:00 +0000)
 - fix buffer overrun in mdw; final '\0' would overflow the output buffer.
 - return ERROR_COMMAND_SYNTAX_ERROR instead of ERROR_OK if:
   - less than one argument is provided
   - the command is called with a name other than mdb, mdh, or mdw.
 - factor all command output into new handle_md_output function

git-svn-id: svn://svn.berlios.de/openocd/trunk@1958 b42882b7-edfa-0310-969c-e2dbd0fdcd60

src/target/target.c

index aa3705a0031de9b4112fdd6202e31d6469103d1c..d56f5e31c67bada06a3e10edf7931217a638e169 100644 (file)
@@ -1844,77 +1844,80 @@ static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, cha
        return ERROR_OK;
 }
 
-static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+static void handle_md_output(struct command_context_s *cmd_ctx,
+               struct target_s *target, u32 address, unsigned size,
+               unsigned count, const u8 *buffer)
 {
-       const int line_bytecnt = 32;
-       int count = 1;
-       int size = 4;
-       u32 address = 0;
-       int line_modulo;
-       int i;
+       const unsigned line_bytecnt = 32;
+       unsigned line_modulo = line_bytecnt / size;
 
-       char output[128];
-       int output_len;
+       char output[line_bytecnt * 4 + 1];
+       unsigned output_len = 0;
 
-       int retval;
+       const char *value_fmt;
+       switch (size) {
+       case 4: value_fmt = "%8.8x"; break;
+       case 2: value_fmt = "%4.2x"; break;
+       case 1: value_fmt = "%2.2x"; break;
+       default:
+               LOG_ERROR("invalid memory read size: %u", size);
+               exit(-1);
+       }
 
-       u8 *buffer;
-       target_t *target = get_current_target(cmd_ctx);
+       for (unsigned i = 0; i < count; i++)
+       {
+               if (i % line_modulo == 0)
+               {
+                       output_len += snprintf(output + output_len,
+                                       sizeof(output) - output_len,
+                                       "0x%8.8x: ", address + (i*size));
+               }
 
-       if (argc < 1)
-               return ERROR_OK;
+               u32 value;
+               const u8 *value_ptr = buffer + i * size;
+               switch (size) {
+               case 4: value = target_buffer_get_u32(target, value_ptr); break;
+               case 2: value = target_buffer_get_u16(target, value_ptr); break;
+               case 1: value = *value_ptr;
+               }
+               output_len += snprintf(output + output_len,
+                               sizeof(output) - output_len,
+                               value_fmt, value);
 
-       if (argc == 2)
-               count = strtoul(args[1], NULL, 0);
+               if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
+               {
+                       command_print(cmd_ctx, "%s", output);
+                       output_len = 0;
+               }
+       }
+}
 
-       address = strtoul(args[0], NULL, 0);
+static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       if (argc < 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
-       switch (cmd[2])
-       {
-               case 'w':
-                       size = 4; line_modulo = line_bytecnt / 4;
-                       break;
-               case 'h':
-                       size = 2; line_modulo = line_bytecnt / 2;
-                       break;
-               case 'b':
-                       size = 1; line_modulo = line_bytecnt / 1;
-                       break;
-               default:
-                       return ERROR_OK;
+       unsigned size = 0;
+       switch (cmd[2]) {
+       case 'w': size = 4; break;
+       case 'h': size = 2; break;
+       case 'b': size = 1; break;
+       default: return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       buffer = calloc(count, size);
-       retval  = target->type->read_memory(target, address, size, count, buffer);
-       if (retval == ERROR_OK)
-       {
-               output_len = 0;
+       u32 address = strtoul(args[0], NULL, 0);
 
-               for (i = 0; i < count; i++)
-               {
-                       if (i%line_modulo == 0)
-                               output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
+       unsigned count = 1;
+       if (argc == 2)
+               count = strtoul(args[1], NULL, 0);
 
-                       switch (size)
-                       {
-                               case 4:
-                                       output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
-                                       break;
-                               case 2:
-                                       output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
-                                       break;
-                               case 1:
-                                       output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
-                                       break;
-                       }
+       u8 *buffer = calloc(count, size);
 
-                       if ((i%line_modulo == line_modulo-1) || (i == count - 1))
-                       {
-                               command_print(cmd_ctx, "%s", output);
-                               output_len = 0;
-                       }
-               }
-       }
+       target_t *target = get_current_target(cmd_ctx);
+       int retval = target->type->read_memory(target,
+                               address, size, count, buffer);
+       if (ERROR_OK == retval)
+               handle_md_output(cmd_ctx, target, address, size, count, buffer);
 
        free(buffer);
 

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)