>From d1ef346afdcd0d1d190693f5d57fbcae7f8944b2 Mon Sep 17 00:00:00 2001 From: Evan Hanson Date: Sun, 2 Dec 2018 14:19:08 +1300 Subject: [PATCH 2/3] Fix empty line and memory leak in debugger GET_TRACE replies Previously, all of the debugger's trace responses would include an empty string in addition to the "real" entries, and the memory allocated for the trace dump itself was never freed. The first issue is fixed by sending only the newline-terminated chunks of the trace buffer (which avoids including the empty string between the final newline and the subsequent null terminator), and the second issue is fixed by simply freeing the trace buffer at the end. --- dbg-stub.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/dbg-stub.c b/dbg-stub.c index 71051f09..feed4980 100644 --- a/dbg-stub.c +++ b/dbg-stub.c @@ -461,23 +461,17 @@ send_event(int event, C_char *loc, C_char *val, C_char *cloc) break; case C_DEBUG_REPLY_GET_TRACE: - str = C_dump_trace(0); - C_strlcpy(rw_buffer, "(* \"", sizeof(rw_buffer)); - ptr = rw_buffer + 4; - - while(*str != '\0') { - if(*str == '\n') { - C_strlcpy(ptr, "\")\n", 4); - send_string(rw_buffer); - C_strlcpy(rw_buffer, "(* \"", sizeof(rw_buffer)); - ptr = rw_buffer + 4; - ++str; - } - else *(ptr++) = *(str++); + str = ptr = C_dump_trace(0); + + while((n = C_strcspn(ptr, "\n"))) { + ptr[ n++ ] = '\0'; + send_string("(* \""); + send_string(ptr); + send_string("\")\n"); + ptr += n; } - C_strlcpy(ptr, "\")\n", 4); - send_string(rw_buffer); + free(str); break; default: terminate("invalid reply code"); -- 2.11.0