public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Bounds check access to Ada task state names
@ 2022-11-16 21:00 Tom Tromey
  2022-11-30 15:45 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2022-11-16 21:00 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

While looking into Ada tasking a little, I noticed that no bounds
checking is done on accesses to the Ada task state names arrays.  This
isn't a problem currently, but if the runtime ever added numbers -- or
if there was some kind of runtime corruption -- it could cause a gdb
crash.

This patch adds range checking.  It also adds a missing _() call when
printing from the 'task_states' array.
---
 gdb/ada-tasks.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index 85d1aaccb06..fbf92041d94 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -85,6 +85,20 @@ static const char * const task_states[] = {
   N_("Selective Wait")
 };
 
+/* Return a string representing the task state.  */
+static const char *
+get_state (unsigned value)
+{
+  if (value >= 0
+      && value <= ARRAY_SIZE (task_states)
+      && task_states[value][0] != '\0')
+    return _(task_states[value]);
+
+  static char buffer[100];
+  xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
+  return buffer;
+}
+
 /* A longer description corresponding to each possible task state.  */
 static const char * const long_task_states[] = {
   N_("Unactivated"),
@@ -107,6 +121,21 @@ static const char * const long_task_states[] = {
   N_("Blocked in selective wait statement")
 };
 
+/* Return a string representing the task state.  This uses the long
+   descriptions.  */
+static const char *
+get_long_state (unsigned value)
+{
+  if (value >= 0
+      && value <= ARRAY_SIZE (long_task_states)
+      && long_task_states[value][0] != '\0')
+    return _(long_task_states[value]);
+
+  static char buffer[100];
+  xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value);
+  return buffer;
+}
+
 /* The index of certain important fields in the Ada Task Control Block
    record and sub-records.  */
 
@@ -1182,7 +1211,7 @@ print_ada_task_info (struct ui_out *uiout,
 			  get_task_number_from_id (task_info->called_task,
 						   inf));
       else
-	uiout->field_string ("state", task_states[task_info->state]);
+	uiout->field_string ("state", get_state (task_info->state));
 
       /* Finally, print the task name, without quotes around it, as mi like
 	 is not expecting quotes, and in non mi-like no need for quotes
@@ -1276,7 +1305,7 @@ info_task (struct ui_out *uiout, const char *taskno_str, struct inferior *inf)
 		    target_taskno);
       }
     else
-      gdb_printf (_("State: %s"), _(long_task_states[task_info->state]));
+      gdb_printf (_("State: %s"), get_long_state (task_info->state));
 
     if (target_taskno)
       {
-- 
2.34.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Bounds check access to Ada task state names
  2022-11-16 21:00 [PATCH] Bounds check access to Ada task state names Tom Tromey
@ 2022-11-30 15:45 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2022-11-30 15:45 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches; +Cc: Tom Tromey

>>>>> "Tom" == Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> While looking into Ada tasking a little, I noticed that no bounds
Tom> checking is done on accesses to the Ada task state names arrays.  This
Tom> isn't a problem currently, but if the runtime ever added numbers -- or
Tom> if there was some kind of runtime corruption -- it could cause a gdb
Tom> crash.

Tom> This patch adds range checking.  It also adds a missing _() call when
Tom> printing from the 'task_states' array.

I'm checking this in now.

Tom

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-30 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-16 21:00 [PATCH] Bounds check access to Ada task state names Tom Tromey
2022-11-30 15:45 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).