Fix crash in "document" command

Message ID 20221118160547.1336252-1-tromey@adacore.com
State New
Headers
Series Fix crash in "document" command |

Commit Message

Tom Tromey Nov. 18, 2022, 4:05 p.m. UTC
  PR cli/29800 points out that "document" will now crash when the
argument is an undefined command.  This is a regression due to the
"document user-defined aliases" patch.
---
 gdb/cli/cli-script.c                | 2 ++
 gdb/testsuite/gdb.base/document.exp | 4 ++++
 2 files changed, 6 insertions(+)
  

Comments

Joel Brobecker Nov. 19, 2022, 12:24 p.m. UTC | #1
Hi Tom,

On Fri, Nov 18, 2022 at 09:05:47AM -0700, Tom Tromey via Gdb-patches wrote:
> PR cli/29800 points out that "document" will now crash when the
> argument is an undefined command.  This is a regression due to the
> "document user-defined aliases" patch.
> ---
>  gdb/cli/cli-script.c                | 2 ++
>  gdb/testsuite/gdb.base/document.exp | 4 ++++
>  2 files changed, 6 insertions(+)
> 
> diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
> index 2442f7ae38b..2101d6fface 100644
> --- a/gdb/cli/cli-script.c
> +++ b/gdb/cli/cli-script.c
> @@ -1515,6 +1515,8 @@ do_document_command (const char *comname, int from_tty,
>    validate_comname (&comname);
>  
>    lookup_cmd_composition (comfull, &alias, &prefix_cmd, &c);
> +  if (c == nullptr)
> +    error (_("Undefined command: \"%s\"."), comfull);
>  
>    if (c->theclass != class_user
>        && (alias == nullptr || alias->theclass != class_alias))
> diff --git a/gdb/testsuite/gdb.base/document.exp b/gdb/testsuite/gdb.base/document.exp
> index 2d8f6b88090..6f426510d29 100644
> --- a/gdb/testsuite/gdb.base/document.exp
> +++ b/gdb/testsuite/gdb.base/document.exp
> @@ -28,3 +28,7 @@ gdb_test_multiple "define do-document" "" {
>  }
>  gdb_test_no_output "do-document" "invoke do-document"
>  gdb_test "help do-document" "usage: do-document" "invoke help do-document"
> +
> +# Test that document of a non-existing command prints an error.  There
> +# was a regression at one point causing this to crash.
> +gdb_test "document nosuchcommand" "Undefined command: \"nosuchcommand\"\\."

Thanks for the fix. This looks good to me!
  
Philippe Waroquiers Nov. 19, 2022, 2:52 p.m. UTC | #2
On Sat, 2022-11-19 at 16:24 +0400, Joel Brobecker via Gdb-patches wrote:
> Hi Tom,
> 
> On Fri, Nov 18, 2022 at 09:05:47AM -0700, Tom Tromey via Gdb-patches wrote:
> > PR cli/29800 points out that "document" will now crash when the
> > argument is an undefined command.  This is a regression due to the
> > "document user-defined aliases" patch.
> > ---
> >  gdb/cli/cli-script.c                | 2 ++
> >  gdb/testsuite/gdb.base/document.exp | 4 ++++
> >  2 files changed, 6 insertions(+)
> > 
> > diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
> > index 2442f7ae38b..2101d6fface 100644
> > --- a/gdb/cli/cli-script.c
> > +++ b/gdb/cli/cli-script.c
> > @@ -1515,6 +1515,8 @@ do_document_command (const char *comname, int from_tty,
> >    validate_comname (&comname);
> >  
> > 
> >    lookup_cmd_composition (comfull, &alias, &prefix_cmd, &c);
> > +  if (c == nullptr)
> > +    error (_("Undefined command: \"%s\"."), comfull);
> >  
> > 
> >    if (c->theclass != class_user
> >        && (alias == nullptr || alias->theclass != class_alias))
> > diff --git a/gdb/testsuite/gdb.base/document.exp b/gdb/testsuite/gdb.base/document.exp
> > index 2d8f6b88090..6f426510d29 100644
> > --- a/gdb/testsuite/gdb.base/document.exp
> > +++ b/gdb/testsuite/gdb.base/document.exp
> > @@ -28,3 +28,7 @@ gdb_test_multiple "define do-document" "" {
> >  }
> >  gdb_test_no_output "do-document" "invoke do-document"
> >  gdb_test "help do-document" "usage: do-document" "invoke help do-document"
> > +
> > +# Test that document of a non-existing command prints an error.  There
> > +# was a regression at one point causing this to crash.
> > +gdb_test "document nosuchcommand" "Undefined command: \"nosuchcommand\"\\."
> 
> Thanks for the fix. This looks good to me!
> 
I also took a look, thanks for fixing the regression
Philippe
  
Tom Tromey Nov. 28, 2022, 1:49 p.m. UTC | #3
>> PR cli/29800 points out that "document" will now crash when the
>> argument is an undefined command.  This is a regression due to the
>> "document user-defined aliases" patch.

Joel> Thanks for the fix. This looks good to me!

Thanks, I'm checking it in.

Tom
  

Patch

diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 2442f7ae38b..2101d6fface 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -1515,6 +1515,8 @@  do_document_command (const char *comname, int from_tty,
   validate_comname (&comname);
 
   lookup_cmd_composition (comfull, &alias, &prefix_cmd, &c);
+  if (c == nullptr)
+    error (_("Undefined command: \"%s\"."), comfull);
 
   if (c->theclass != class_user
       && (alias == nullptr || alias->theclass != class_alias))
diff --git a/gdb/testsuite/gdb.base/document.exp b/gdb/testsuite/gdb.base/document.exp
index 2d8f6b88090..6f426510d29 100644
--- a/gdb/testsuite/gdb.base/document.exp
+++ b/gdb/testsuite/gdb.base/document.exp
@@ -28,3 +28,7 @@  gdb_test_multiple "define do-document" "" {
 }
 gdb_test_no_output "do-document" "invoke do-document"
 gdb_test "help do-document" "usage: do-document" "invoke help do-document"
+
+# Test that document of a non-existing command prints an error.  There
+# was a regression at one point causing this to crash.
+gdb_test "document nosuchcommand" "Undefined command: \"nosuchcommand\"\\."