From d9b2607ca094898d8d7180085a913e8a5b46ecac Mon Sep 17 00:00:00 2001 From: Ian Thompson Date: Mon, 1 Aug 2022 15:47:52 -0700 Subject: [PATCH] gdb_server: support sparse register maps Add additional error handling for targets where gaps may exist in reg_list[] Signed-off-by: Ian Thompson Change-Id: I65232429e2de08f5d54eeca53aea0db8ce2b58af Reviewed-on: https://review.openocd.org/c/openocd/+/7103 Tested-by: jenkins Reviewed-by: Antonio Borneo --- src/server/gdb_server.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index 28833c9ce..3052d0a0e 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -1323,6 +1323,8 @@ static int gdb_set_registers_packet(struct connection *connection, packet_p = packet; for (i = 0; i < reg_list_size; i++) { uint8_t *bin_buf; + if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden) + continue; int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2); if (packet_p + chars > packet + packet_size) @@ -1375,7 +1377,8 @@ static int gdb_get_register_packet(struct connection *connection, if (retval != ERROR_OK) return gdb_error(connection, retval); - if (reg_list_size <= reg_num) { + if ((reg_list_size <= reg_num) || !reg_list[reg_num] || + !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) { LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num); return ERROR_SERVER_REMOTE_CLOSED; } @@ -1437,7 +1440,8 @@ static int gdb_set_register_packet(struct connection *connection, return gdb_error(connection, retval); } - if (reg_list_size <= reg_num) { + if ((reg_list_size <= reg_num) || !reg_list[reg_num] || + !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) { LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num); free(bin_buf); free(reg_list);