diff options
author | Andrew Burgess <aburgess@redhat.com> | 2025-04-11 16:01:02 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2025-05-13 14:22:22 +0100 |
commit | 3e87f196c5c35e3e071ef0cdd14666d2c97d3f51 (patch) | |
tree | c4e1be8f6672b4e4e3e36bb632b199315311b574 /gdb/python | |
parent | 614806c7c2a3978211f65c13f30b5a90ee9e814d (diff) | |
download | binutils-3e87f196c5c35e3e071ef0cdd14666d2c97d3f51.zip binutils-3e87f196c5c35e3e071ef0cdd14666d2c97d3f51.tar.gz binutils-3e87f196c5c35e3e071ef0cdd14666d2c97d3f51.tar.bz2 |
gdb/python/guile: check for invalid prefixes in Command/Parameter creation
The manual for gdb.Parameter says:
If NAME consists of multiple words, and no prefix parameter group
can be found, an exception is raised.
This makes sense; we cannot create a parameter within a prefix group,
if the prefix doesn't exist. And this almost works, so:
(gdb) python gdb.Parameter("xxx foo", gdb.COMMAND_NONE, gdb.PARAM_BOOLEAN)
Python Exception <class 'RuntimeError'>: Could not find command prefix xxx.
Error occurred in Python: Could not find command prefix xxx.
The prefix 'xxx' doesn't exist, and we get an error. But, if we try
multiple levels of prefix:
(gdb) python gdb.Parameter("print xxx foo", gdb.COMMAND_NONE, gdb.PARAM_BOOLEAN)
This completes without error, however, we didn't get what we were
maybe expecting:
(gdb) show print xxx foo
Undefined show print command: "xxx foo". Try "help show print".
But we did get:
(gdb) show print foo
The current value of 'print foo' is "off".
GDB stopped scanning the prefix string at the unknown 'xxx', and just
created the parameter there. I don't think this makes sense, nor is
it inline with the manual.
An identical problem exists with gdb.Command creation; GDB stops
parsing the prefix at the first unknown prefix, and just creates the
command there. The manual for gdb.Command says:
NAME is the name of the command. If NAME consists of multiple
words, then the initial words are looked for as prefix commands.
In this case, if one of the prefix commands does not exist, an
exception is raised.
So again, the correct action is, I believe, to raise an exception.
The problem is in gdbpy_parse_command_name (python/py-cmd.c), GDB
calls lookup_cmd_1 to look through the prefix string and return the
last prefix group. If the very first prefix word is invalid then
lookup_cmd_1 returns NULL, and this case is handled. However, if
there is a valid prefix, followed by an invalid prefix, then
lookup_cmd_1 will return a pointer to the last valid prefix list, and
will update the input argument to point to the start of the invalid
prefix word. This final case, where the input is left pointing to an
unknown prefix, was previously not handled.
I've fixed gdbpy_parse_command_name, and added tests for command and
parameter creation to cover this case.
The exact same error is present in the guile API too. The guile
documentation for make-parameter and make-command says the same things
about unknown prefixes resulting in an exception, but the same error
is present in gdbscm_parse_command_name (guile/scm-cmd.c), so I've
fixed that too, and added some tests.
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/py-cmd.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 5d98d03..c53138a 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -385,7 +385,7 @@ gdbpy_parse_command_name (const char *name, prefix_text2 = prefix_text.c_str (); elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1); - if (elt == NULL || elt == CMD_LIST_AMBIGUOUS) + if (elt == nullptr || elt == CMD_LIST_AMBIGUOUS || *prefix_text2 != '\0') { PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."), prefix_text.c_str ()); |