From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 558D43858CDA for ; Mon, 29 Jan 2024 13:19:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 558D43858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 558D43858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706534367; cv=none; b=YMnlJsXZ8GYtIM0uRPkCVW0k904AdBlyCu3G3/5nInTcyFPz/cQehTa/xYG1HzkJ8lfdjawtfb6b0Y+ZKdMsm//hk30B1dcZwVZW0GNbWf6VYE0DY9GV+s8SDrTpT21K1tPrFE1ekNGNL4hox0tzPwJpMh374tdsOhoNGD2VA4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706534367; c=relaxed/simple; bh=agXCtnRBVYOO0M17PvQENIyY3ufxjndpGVK6RCnIyR4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=heI2N8ajyc/SrmcpUTgzLmZDT10oNWPYa7GWvagw4VVrmmkWXgmWdVvV5pBWuiN+PDfHtwadt8Tt4fxWe4Z1M9vGnbh+pUj/KTc+yFTvXii80sm6ainZ8trrK9Cquq6pyeASV/emqxJKgIX0rsL7qMWfKlDOPrcIdMsFjGm3xZM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706534361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Aymc17dOaEC5S7i0nWqay6kKcJm0WT58GS9uZR9GJTs=; b=jRezqEgDsgAgd9vrVV+gxzZcncVldWXBiFazg1WJesIov3eqdjHt8TzKk2uNfO7PKE+Qi7 LNYliQqMNb4miEV7/1kYYd/yCV7JVCwmRuoLwiOdIBk2fixjOW0qgn4nl8tchPRdGKzyDi TvoqhymZijMU2vk528iULAu31Q71QOw= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-pBj3bV1UNeC9BTXoueZrSg-1; Mon, 29 Jan 2024 08:19:20 -0500 X-MC-Unique: pBj3bV1UNeC9BTXoueZrSg-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a35118e98caso179006866b.0 for ; Mon, 29 Jan 2024 05:19:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706534359; x=1707139159; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Aymc17dOaEC5S7i0nWqay6kKcJm0WT58GS9uZR9GJTs=; b=q3PJ6s6rZTJsqs73QzxQkgyzJJ0zkc/SCgBPeT6SZmcMp5CTw4WZYVhRBFTvPiXOBH PzK2nYESoen4OwOQtT3pIy8MrOEovQfdpjv7THIWZDmFtPnwENJLu97JMkRukDPhUwRx SaE1wzGe5A62f1B6byhnIX8p38Vzq3n0qPc9/8p4I596yWI3CPT0XYVMaRvb7F9/cwNE weWbOD02rQLxlmsQIuSqjgnY4HXD/adUOnjcMsVW9vYvL2th8f0xkFwdvpczkyneOrPu T0FQWAKv0ylCEMaaTZPqvVnfKMDt6WRtCMQuw2RbFZ7bGXQEHAD6xz0cqva7BG7z5R87 Yo5g== X-Gm-Message-State: AOJu0YxjwaczwDFssgXHlSViKPROyiLbGDkMox1tnJnhz3fvHVYH0IwA ejnU3+1TErA6sYqr/lKOJY2FUbixDohgZm1CV9bc3n0xDqMpYfRplknLoVc1wWfzRAlwCLrrtKv nWOu/Ql07cIviXI6d0ahGzei/9yb74ng8JMx9r2VmSjCFdKDIVr8eVuHn0Sw= X-Received: by 2002:a17:906:3413:b0:a35:9c78:8a12 with SMTP id c19-20020a170906341300b00a359c788a12mr2642393ejb.70.1706534359172; Mon, 29 Jan 2024 05:19:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZUPfPZ0CSO8Q7U0XA+nyuL/P1H2pb/ylA6rHZpXhc79VgpbtAqPcH+kVwZa4R74vbTinz+g== X-Received: by 2002:a17:906:3413:b0:a35:9c78:8a12 with SMTP id c19-20020a170906341300b00a359c788a12mr2642378ejb.70.1706534358754; Mon, 29 Jan 2024 05:19:18 -0800 (PST) Received: from localhost (92.40.215.5.threembb.co.uk. [92.40.215.5]) by smtp.gmail.com with ESMTPSA id p2-20020a17090628c200b00a311092d2f8sm3900661ejd.169.2024.01.29.05.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 05:19:18 -0800 (PST) From: Andrew Burgess To: Hannes Domani , gdb-patches@sourceware.org Subject: Re: [PATCH v2] Raise exception if ambiguous name is used in gdb.parameter In-Reply-To: <20231219163851.2748-1-ssbssa@yahoo.de> References: <20231219163851.2748-1-ssbssa.ref@yahoo.de> <20231219163851.2748-1-ssbssa@yahoo.de> Date: Mon, 29 Jan 2024 13:19:16 +0000 Message-ID: <87sf2gxoob.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hannes Domani writes: > Currently gdb.parameter doesn't raise an exception 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, and by setting *CMD to the special value > CMD_LIST_AMBIGUOUS in this case, gdbpy_parameter can now show > the appropriate error message: > ``` > (gdb) py print(gdb.parameter("print sym")) > Traceback (most recent call last): > File "", line 1, in > RuntimeError: Parameter `print sym' is ambiguous. > 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 "", line 1, in > RuntimeError: Parameter `print symbol-' is ambiguous. > Error while executing Python code. > (gdb) py print(gdb.parameter("print symbol-load")) > full > ``` > > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14639 > --- > v2: > - Small changes in commit message. > - Use CMD_LIST_AMBIGUOUS in lookup_cmd_composition_1 to mark > ambiguous commands, so gdbpy_parameter can show a better > error message. > --- > gdb/cli/cli-decode.c | 13 ++++++--- > gdb/python/python.c | 5 +++- > gdb/testsuite/gdb.python/py-parameter.exp | 32 +++++++++++++++++++++++ > 3 files changed, 46 insertions(+), 4 deletions(-) > > diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c > index 940cd6a2c8e..db57764578d 100644 > --- a/gdb/cli/cli-decode.c > +++ b/gdb/cli/cli-decode.c > @@ -2549,7 +2549,8 @@ deprecated_cmd_warning (const char *text, struct cmd_list_element *list) > If TEXT is a subcommand (i.e. one that is preceded by a prefix > command) set *PREFIX_CMD. > > - Set *CMD to point to the command TEXT indicates. > + Set *CMD to point to the command TEXT indicates, or to > + CMD_LIST_AMBIGUOUS if there are multiple possible matches. > > If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not > exist, they are NULL when we return. > @@ -2589,7 +2590,12 @@ 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 = CMD_LIST_AMBIGUOUS; > + return 0; This changes the API of lookup_cmd_composition. You need to audit all the calls to this function to check that non are depending on CMD being set to nullptr. I only looked at one in cli/cli-script.c in the function do_document_command that is a problem, try this: (gdb) define foo_1 Type commands for definition of "foo_1". End with a line saying just "end". >echo aa\n >end (gdb) define foo_2 Type commands for definition of "foo_2". End with a line saying just "end". >echo bb\n >end (gdb) document foo Fatal signal: Segmentation fault ... etc ... Not every use of lookup_cmd_composition is a problem, e.g. in alias_command in cli/cli-cmds.c it's not an issue, so you'll need to check 'em all. If you do find cases that would have triggered bugs with this change, it would be awesome if you could add tests to cover these cases, though I guess that's not a hard requirement. Thanks, Andrew > + } > + else if (*cmd == nullptr) > return 0; > else > { > @@ -2623,7 +2629,8 @@ lookup_cmd_composition_1 (const char *text, > If TEXT is a subcommand (i.e. one that is preceded by a prefix > command) set *PREFIX_CMD. > > - Set *CMD to point to the command TEXT indicates. > + Set *CMD to point to the command TEXT indicates, or to > + CMD_LIST_AMBIGUOUS if there are multiple possible matches. > > If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not > exist, they are NULL when we return. > diff --git a/gdb/python/python.c b/gdb/python/python.c > index 951de82ef06..fa0b1ca6604 100644 > --- a/gdb/python/python.c > +++ b/gdb/python/python.c > @@ -559,7 +559,10 @@ gdbpy_parameter (PyObject *self, PyObject *args) > GDB_PY_HANDLE_EXCEPTION (ex); > } > > - if (!found) > + if (cmd == CMD_LIST_AMBIGUOUS) > + return PyErr_Format (PyExc_RuntimeError, > + _("Parameter `%s' is ambiguous."), arg); > + else if (!found) > return PyErr_Format (PyExc_RuntimeError, > _("Could not find parameter `%s'."), arg); > > diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp > index c1f8a80c6b7..828fe84694d 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'))" \ > + "Parameter .* is ambiguous.*Error while executing Python code." > + gdb_test "python print(gdb.parameter('test-ambiguous-'))" \ > + "Parameter .* is ambiguous.*Error while executing Python code." > + gdb_test "python print(gdb.parameter('test-ambiguous-v'))" \ > + "Parameter .* is ambiguous.*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