gdb_server: use strndup to allocate debug messages
[openocd.git] / src / server / gdb_server.c
index cbc2b401c37596cd8134ea8b313dd2acf9d1ada9..537513a1b6b831179ac8a405ef02d44170fa1e75 100644 (file)
@@ -44,7 +44,6 @@
 #include "rtos/rtos.h"
 #include "target/smp.h"
 
-
 /**
  * @file
  * GDB server implementation.
@@ -77,7 +76,6 @@ struct gdb_connection
        bool mem_write_error;
 };
 
-
 #if 0
 #define _DEBUG_GDB_IO_
 #endif
@@ -245,9 +243,7 @@ static int gdb_get_char_inner(struct connection *connection, int* next_char)
        }
 
 #ifdef _DEBUG_GDB_IO_
-       debug_buffer = malloc(gdb_con->buf_cnt + 1);
-       memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
-       debug_buffer[gdb_con->buf_cnt] = 0;
+       debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
        LOG_DEBUG("received '%s'", debug_buffer);
        free(debug_buffer);
 #endif
@@ -389,9 +385,7 @@ static int gdb_put_packet_inner(struct connection *connection,
        while (1)
        {
 #ifdef _DEBUG_GDB_IO_
-               debug_buffer = malloc(len + 1);
-               memcpy(debug_buffer, buffer, len);
-               debug_buffer[len] = 0;
+               debug_buffer = strndup(buffer, len);
                LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
                free(debug_buffer);
 #endif
@@ -1058,12 +1052,12 @@ static int gdb_get_registers_packet(struct connection *connection,
 
        for (i = 0; i < reg_list_size; i++)
        {
-               reg_packet_size += reg_list[i]->size;
+               reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
        }
 
        assert(reg_packet_size > 0);
 
-       reg_packet = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
+       reg_packet = malloc(reg_packet_size);
        reg_packet_p = reg_packet;
 
        for (i = 0; i < reg_list_size; i++)
@@ -1076,14 +1070,14 @@ static int gdb_get_registers_packet(struct connection *connection,
 
 #ifdef _DEBUG_GDB_IO_
        {
-               char *reg_packet_p;
-               reg_packet_p = strndup(reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
-               LOG_DEBUG("reg_packet: %s", reg_packet_p);
-               free(reg_packet_p);
+               char *reg_packet_p_debug;
+               reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
+               LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
+               free(reg_packet_p_debug);
        }
 #endif
 
-       gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
+       gdb_put_packet(connection, reg_packet, reg_packet_size);
        free(reg_packet);
 
        free(reg_list);

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)