public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] Throw error if ambiguous name is used in gdb.parameter
Date: Sat, 16 Dec 2023 13:56:00 +0100	[thread overview]
Message-ID: <20231216125600.3592-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20231216125600.3592-1-ssbssa.ref@yahoo.de>

Currently gdb.parameter doesn't throw an error if an ambiguous
name is used, it instead returns the value of the last partly
matching parameter:
```
(gdb) show print sym
Ambiguous show print command "sym": symbol, symbol-filename, symbol-loading.
(gdb) show print symbol-loading
Printing of symbol loading messages is "full".
(gdb) py print(gdb.parameter("print sym"))
full
```

It's because lookup_cmd_composition_1 tries to detect
ambigous names by checking the return value of find_cmd
for CMD_LIST_AMBIGUOUS, which never happens, since only
lookup_cmd_1 returns CMD_LIST_AMBIGUOUS.
Instead the nfound argument contains the number of found
matches.

By using it instead, it's working/failing as expected:
```
(gdb) py print(gdb.parameter("print sym"))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: Could not find parameter `print sym'.
Error while executing Python code.
(gdb) py print(gdb.parameter("print symbol"))
True
(gdb) py print(gdb.parameter("print symbol-"))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError: Could not find parameter `print symbol-'.
Error while executing Python code.
(gdb) py print(gdb.parameter("print symbol-load"))
full
```

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14639
---
 gdb/cli/cli-decode.c                      |  2 +-
 gdb/testsuite/gdb.python/py-parameter.exp | 32 +++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 940cd6a2c8e..12aa152d0c4 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -2589,7 +2589,7 @@ lookup_cmd_composition_1 (const char *text,
       *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
 
       /* We only handle the case where a single command was found.  */
-      if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
+      if (nfound > 1 || *cmd == nullptr)
 	return 0;
       else
 	{
diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp
index c1f8a80c6b7..df1b93028ad 100644
--- a/gdb/testsuite/gdb.python/py-parameter.exp
+++ b/gdb/testsuite/gdb.python/py-parameter.exp
@@ -588,6 +588,37 @@ proc_with_prefix test_language {} {
     gdb_test_no_output "set lang auto"
 }
 
+proc_with_prefix test_ambiguous_parameter {} {
+    gdb_test_multiline "create parameter" \
+	"python" "" \
+	"class TestAmbiguousParam (gdb.Parameter):" "" \
+	"   def __init__ (self, name, value):" "" \
+	"      super (TestAmbiguousParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_INTEGER)" "" \
+	"      self.value = value" "" \
+	"end"
+
+    # Create parameters.
+    gdb_test "python TestAmbiguousParam('test-ambiguous-value-1', 1)" ""
+    gdb_test "python TestAmbiguousParam('test-ambiguous-value-2-extra', 2)" ""
+    gdb_test "python TestAmbiguousParam('test-ambiguous', 3)" ""
+
+    # Test unambiguous matches.
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-1'))" "1"
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-2-extra'))" "2"
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-2'))" "2"
+    gdb_test "python print(gdb.parameter('test-ambiguous'))" "3"
+
+    # Test ambiguous names.
+    gdb_test "python print(gdb.parameter('test-ambiguou'))" \
+	"Could not find parameter.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-'))" \
+	"Could not find parameter.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-v'))" \
+	"Could not find parameter.*Error while executing Python code."
+    gdb_test "python print(gdb.parameter('test-ambiguous-value-1a'))" \
+	"Could not find parameter.*Error while executing Python code."
+}
+
 test_directories
 test_data_directory
 test_boolean_parameter
@@ -600,5 +631,6 @@ test_gdb_parameter
 test_integer_parameter
 test_throwing_parameter
 test_language
+test_ambiguous_parameter
 
 rename py_param_test_maybe_no_output ""
-- 
2.35.1


       reply	other threads:[~2023-12-16 12:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20231216125600.3592-1-ssbssa.ref@yahoo.de>
2023-12-16 12:56 ` Hannes Domani [this message]
2023-12-19 13:23   ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231216125600.3592-1-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).