From 1e427fd580ad94bb3bf29d4e02ef81361c920f28 Mon Sep 17 00:00:00 2001 From: Paul Fertser Date: Wed, 6 Nov 2019 19:04:57 +0300 Subject: [PATCH] server: gdb_server: fix memory map generation on a 32-bit BE host Due to lack of printf format check wrong specifier was used and it actually broke operation on a 32-bit BE host. So fix this and add the necessary function attributes so that the bugs like that can be uncovered automaticaly. Reported and pinpointed by Karl Palsson on IRC. Change-Id: I254ec28fcd9bb30594d607f74a6dba5456c2c7a1 Tested-by: Karl Palsson Signed-off-by: Paul Fertser Reviewed-on: http://openocd.zylin.com/5342 Tested-by: jenkins Reviewed-by: Karl Palsson Reviewed-by: Antonio Borneo --- src/helper/log.h | 3 ++- src/server/gdb_server.c | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/helper/log.h b/src/helper/log.h index d60587f72..bc08fe49a 100644 --- a/src/helper/log.h +++ b/src/helper/log.h @@ -95,7 +95,8 @@ int log_add_callback(log_callback_fn fn, void *priv); int log_remove_callback(log_callback_fn fn, void *priv); char *alloc_vprintf(const char *fmt, va_list ap); -char *alloc_printf(const char *fmt, ...); +char *alloc_printf(const char *fmt, ...) + __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 1, 2))); extern int debug_level; diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index c8f0e523c..49939a5cb 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -1729,8 +1729,8 @@ static int gdb_breakpoint_watchpoint_packet(struct connection *connection, /* print out a string and allocate more space as needed, * mainly used for XML at this point */ -static void xml_printf(int *retval, char **xml, int *pos, int *size, - const char *fmt, ...) +static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(int *retval, + char **xml, int *pos, int *size, const char *fmt, ...) { if (*retval != ERROR_OK) return; @@ -1871,7 +1871,7 @@ static int gdb_memory_map(struct connection *connection, if (ram_start < p->base) xml_printf(&retval, &xml, &pos, &size, "\n", + "length=\"" TARGET_ADDR_FMT "\"/>\n", ram_start, p->base - ram_start); /* Report adjacent groups of same-size sectors. So for @@ -2469,7 +2469,7 @@ static int gdb_generate_thread_list(struct target *target, char **thread_list_ou xml_printf(&retval, &thread_list, &pos, &size, ", "); xml_printf(&retval, &thread_list, &pos, &size, - thread_detail->extra_info_str); + "%s", thread_detail->extra_info_str); } xml_printf(&retval, &thread_list, &pos, &size, @@ -3555,7 +3555,7 @@ static int gdb_target_add_one(struct target *target) if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) { free(gdb_port_next); if (portnumber) { - gdb_port_next = alloc_printf("%d", portnumber+1); + gdb_port_next = alloc_printf("%ld", portnumber+1); } else { /* Don't increment if gdb_port is 0, since we're just * trying to allocate an unused port. */