public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix crash with "maintenance print arc"
@ 2022-04-27 10:07 Luis Machado
  2022-04-27 10:43 ` Andrew Burgess
  2022-04-28 15:26 ` Tom Tromey
  0 siblings, 2 replies; 12+ messages in thread
From: Luis Machado @ 2022-04-27 10:07 UTC (permalink / raw)
  To: gdb-patches

While doing something else, I noticed GDB crashed with
"maintenance print arc".

This happens because the code expects to find a "show" string pattern
within "maintenance print arc", since "arc" here is a prefix, and skip it.
In this case though, it won't find it, and we will have a bad pointer
getting dereferenced.

There is another part of the code with a similar assumption.

This patch hardens both code paths to prevent future crashes.

Regression-tested on x86_64 and aarch64 Linux Ubuntu 20.04.
---
 gdb/cli/cli-setshow.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c
index 213573e443e..3852a505cf5 100644
--- a/gdb/cli/cli-setshow.c
+++ b/gdb/cli/cli-setshow.c
@@ -712,7 +712,13 @@ cmd_show_list (struct cmd_list_element *list, int from_tty)
 	{
 	  ui_out_emit_tuple optionlist_emitter (uiout, "optionlist");
 	  std::string prefixname = list->prefixname ();
-	  const char *new_prefix = strstr (prefixname.c_str (), "show ") + 5;
+	  const char *new_prefix = strstr (prefixname.c_str (), "show ");
+
+	  /* If we've found a "show" string, remove it now.  */
+	  if (new_prefix != nullptr)
+	    new_prefix += 5;
+	  else
+	    new_prefix = prefixname.c_str ();
 
 	  if (uiout->is_mi_like_p ())
 	    uiout->field_string ("prefix", new_prefix);
@@ -726,8 +732,15 @@ cmd_show_list (struct cmd_list_element *list, int from_tty)
 	    {
 	      /* If we find a prefix, output it (with "show " skipped).  */
 	      std::string prefixname = list->prefix->prefixname ();
-	      prefixname = (!list->prefix->is_prefix () ? ""
-			    : strstr (prefixname.c_str (), "show ") + 5);
+	      const char *prefix = nullptr;
+
+	      if (list->prefix->is_prefix ())
+		prefix = strstr (prefixname.c_str (), "show ");
+
+	      /* If we've found a "show" string, remove it now.  */
+	      if (prefix != nullptr)
+		prefixname = prefix + 5;
+
 	      uiout->text (prefixname);
 	    }
 	  uiout->field_string ("name", list->name);
-- 
2.25.1


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

end of thread, other threads:[~2022-05-27 13:35 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27 10:07 [PATCH] Fix crash with "maintenance print arc" Luis Machado
2022-04-27 10:43 ` Andrew Burgess
2022-04-27 11:00   ` Luis Machado
2022-04-28 15:27   ` Tom Tromey
2022-04-29 10:20     ` Andrew Burgess
2022-04-28 15:26 ` Tom Tromey
2022-04-29  8:58   ` Luis Machado
2022-04-29 10:18     ` Andrew Burgess
2022-04-29 10:39       ` Luis Machado
2022-04-29 11:25         ` Pedro Alves
2022-05-05  9:31   ` Luis Machado
2022-05-27 13:34     ` 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).