From patchwork Wed Mar 6 10:23:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86865 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E8DB385802F for ; Wed, 6 Mar 2024 10:29:45 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E54873858029 for ; Wed, 6 Mar 2024 10:29:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E54873858029 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 E54873858029 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709720951; cv=none; b=YSHcIODoOH7xHBp40Ndda+13jpEOTn6uURdokYeArVNy4Z2HFsPmLwiU9F3sjko7hNPY4E7R1leVk3styg/kEMYu6g6Cwt7sWZx5snHJwVgldXc+3XqmkUsnvF4olPpq4ndkNCUWNSgCOpg782nEmyzYrTGGnJ3Pokc0tvfl7B4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709720951; c=relaxed/simple; bh=SHgBJ1Z5DpjNUF94EAGELUQvQAk/bMx0KEYtPumGlls=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=jmzTWhk6mjWlNC5xTsC38XgkHACf7aLlhBQEIDOED22qrxC3u/x2kvvvcmFSKPtXCF/O4GT33PLg2pNthJ6OyBlPReJ5Zk2wZuo5WSAmUubuPWdZOkh+9VAuUM1tkBxoM+uNofoQXvx/tfkpDCpxGNSe3N1K22ZlBVg64/wRA3c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709720948; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ps/DzOdiWY5rI9PxpgIJ3G1NvkScu8mmjx0fQOnxP9A=; b=AAqUpYYr+mvypFkwJ3rCqQAcplGKNk4qP3RsgzU7Y/DbGOxUsbnQ5w3GkvynV4dN/f8dfc IBShqoGRuLjtES0IEe47M4xfzm6y0nAXkiAC3vbQPKopa1MBxsRy1FTdaKKLm9lOEDhe30 0vZ0Fqpoogk1U9uWf4US/BgovsUTqPo= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-468-uc0wFu7lMxqaAaGv42WWDA-1; Wed, 06 Mar 2024 05:29:07 -0500 X-MC-Unique: uc0wFu7lMxqaAaGv42WWDA-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a3fcf5b93faso436066866b.2 for ; Wed, 06 Mar 2024 02:29:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709720634; x=1710325434; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bozw9Yio2RMn1fX9HaTA0jiNm3rZBiOhqDHqLaDQkIM=; b=E0hZb12xcd0w9qDQrQJgxbjhpIO6jgSk0b0C068a31ukMsQ9OKVqdkXIuvUHYk4D68 /QApb5AaKZKkaSI7r0dW7vsKt0UpENGVELZwhOTLpg2Us1yfCptgd++GJpOC7m5v0QaL dKcvlY/T2Sg+PRpBtYNRWJBR09qY67sngrbCWA7tGbG/fTv5QV+VYoHwnl+JqU0XVHOy UakH+wsnyBz8yZ5HZXjbLuhxENL5MjFD/9pwuTpxAKCr+r/69iQVJ7ggIVW+OzE6hdq+ AmIROZVO+p0J/DbBw5D4M7EaAE5kTmDfMqmYs14bUvgKitCzLQzv2LPhJ2zdjNUxaM5f M+cw== X-Gm-Message-State: AOJu0YxDK6hmstTG/0bWBkL1RLGlbGOyC9+rmalrMjp6/QyIqxzIKSvY 1NUN+BhKFZfRjBZ57U1Mj1PYnQRX7nGSWOJBkx9+bZ09XLSKfjQMJIhTXC77W5MsqMQX/rkULs/ KAuXAdGuyj490PIS6Qg8dMWk9SDwK1QsGw1d0tC9ZsOVxkNjJqE+PZRK/QQ7qsZVBjYwJMUAZqt C+ak6uwda07t7Nr8FWGB58PhLauEibscPKFSNmbS6yIaU= X-Received: by 2002:a17:906:390e:b0:a44:446b:e99e with SMTP id f14-20020a170906390e00b00a44446be99emr9241303eje.31.1709720633624; Wed, 06 Mar 2024 02:23:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IHxi/4hxkVJsgy/44eq3x0vXPFoaNrOkUbtc9obJMmiyIInTEH9dLe9MGXlc+ujAcJAmDLxPw== X-Received: by 2002:a17:906:390e:b0:a44:446b:e99e with SMTP id f14-20020a170906390e00b00a44446be99emr9241284eje.31.1709720632870; Wed, 06 Mar 2024 02:23:52 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id sa20-20020a1709076d1400b00a4432543b21sm6934134ejc.198.2024.03.06.02.23.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 02:23:52 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/7] gdb: fix bug where quote characters would become nullptr Date: Wed, 6 Mar 2024 10:23:34 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org In gdb_completion_word_break_characters_throw, after calling complete_line_internal, if the completion function chose to use a custom word point then we set rl_completer_quote_characters to NULL. However, nowhere do we set rl_completer_quote_characters back to its default value, which is setup in init_main (top.c). An example of something that uses a custom word point for its completion is 'thread apply all ...'. An example of something that relies on rl_completer_quote_characters would be completion of a quoted filename that contains white space. Consider this shell and GDB session. The markers indicate where I've used tab to trigger completion: $ mkdir /tmp/aaa\ bbb $ touch /tmp/aaa\ bbb/xx\ 11 $ touch /tmp/aaa\ bbb/xx\ 22 $ gdb -q (gdb) file '/tmp/aaa bbb/xx xx 11 xx 22 (gdb) thread apply all hel (gdb) thread apply all help (gdb) file '/tmp/aaa bbb/xx First I create a directory structure which uses white space within file and directory names. Then within GDB I use the 'file' command and use a single quote to quote the filename. When I tab complete GDB correctly offers the two files within the directory '/tmp/aaa bbb/'. This works because rl_completer_quote_characters contains the single quote, and so readline knows that it is trying to complete the string that starts after the single quote: /tmp/aaa bbb/xx Next I invoke the completer for the 'thread apply all' command, to do this I type 'thread apply all hel' and hit tab, this expands to the one completion 'thread apply all help'. We can run this command or not, it doesn't matter (there are no threads, so we'll get no output). Now I repeat the original 'file' completion. This time though I don't get offered any completions. The reason is that the 'thread apply all' completer set rl_completer_quote_characters to nullptr. Now, when readline tries to figure out the word to complete it doesn't see the single quote as the start of a quoted word, so instead readline falls back to the word break characters, and in this case spots the white space. As a result readline tries to complete the string 'bbb/xx' which obviously doesn't have any completions. By setting rl_completer_quote_characters each time completion is invoked this problem is resolved and the second 'file' command completes as expected. I've extended gdb.base/filename-completion.exp to also test with quoted filenames, and added a 'thread apply all' completion at the start to expose this bug. As setting of rl_completer_quote_characters is now all done in the completer.c file the function get_gdb_completer_quote_characters() could be made static. However, as this function is only used one time to initialise rl_completer_quote_characters, I've instead just deleted get_gdb_completer_quote_characters() and used gdb_completer_quote_characters directly. --- gdb/completer.c | 10 +-- gdb/completer.h | 2 - .../gdb.base/filename-completion.exp | 66 +++++++++++++------ gdb/top.c | 1 - 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index 8168f79de0e..44da6548eb4 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -181,13 +181,6 @@ static const char gdb_completer_file_name_break_characters[] = sequences as strings. */ static const char gdb_completer_quote_characters[] = "'"; -/* Accessor for some completer data that may interest other files. */ - -const char * -get_gdb_completer_quote_characters (void) -{ - return gdb_completer_quote_characters; -} /* This can be used for functions which don't want to complete on symbols but don't want to complete on anything else either. */ @@ -1269,6 +1262,9 @@ complete_line_internal_1 (completion_tracker &tracker, set_rl_completer_word_break_characters (current_language->word_break_characters ()); + /* Likewise for the quote characters. */ + rl_completer_quote_characters = gdb_completer_quote_characters; + /* Decide whether to complete on a list of gdb commands or on symbols. */ tmp_command = (char *) alloca (point + 1); diff --git a/gdb/completer.h b/gdb/completer.h index f604a95011f..0a95ced8a80 100644 --- a/gdb/completer.h +++ b/gdb/completer.h @@ -619,8 +619,6 @@ extern void reggroup_completer (struct cmd_list_element *, completion_tracker &tracker, const char *, const char *); -extern const char *get_gdb_completer_quote_characters (void); - extern char *gdb_completion_word_break_characters (void); /* Set the word break characters array to BREAK_CHARS. This function diff --git a/gdb/testsuite/gdb.base/filename-completion.exp b/gdb/testsuite/gdb.base/filename-completion.exp index 20ca227f96d..7d8ab1a3350 100644 --- a/gdb/testsuite/gdb.base/filename-completion.exp +++ b/gdb/testsuite/gdb.base/filename-completion.exp @@ -37,6 +37,9 @@ proc setup_directory_tree {} { remote_exec host "mkdir -p ${root}/cc1" remote_exec host "touch ${root}/cc2" + remote_exec host "touch \"${root}/aaa/aa bb\"" + remote_exec host "touch \"${root}/aaa/aa cc\"" + return $root } @@ -45,26 +48,49 @@ proc setup_directory_tree {} { # sub-directory of the user's home directory ROOT might have been # modified to replace the $HOME prefix with a single "~" character. proc run_tests { root } { - test_gdb_complete_none "file ${root}/xx" \ - "expand a non-existent filename" - - test_gdb_complete_unique "file ${root}/a" \ - "file ${root}/aaa/" "" false \ - "expand a unique filename" - - test_gdb_complete_multiple "file ${root}/" \ - "b" "b" { - "bb1/" - "bb2/" - } "" "" false \ - "expand multiple directory names" - - test_gdb_complete_multiple "file ${root}/" \ - "c" "c" { - "cc1/" - "cc2" - } "" "" false \ - "expand mixed directory and file names" + + # Completing 'thread apply all ...' commands uses a custom word + # point. At one point we had a bug where doing this would break + # completion of quoted filenames that contained white space. + test_gdb_complete_unique "thread apply all hel" \ + "thread apply all help" " " false \ + "complete a 'thread apply all' command" + + foreach_with_prefix qc [list "" "'"] { + test_gdb_complete_none "file ${qc}${root}/xx" \ + "expand a non-existent filename" + + test_gdb_complete_unique "file ${qc}${root}/a" \ + "file ${qc}${root}/aaa/" "" false \ + "expand a unique filename" + + test_gdb_complete_multiple "file ${qc}${root}/" \ + "b" "b" { + "bb1/" + "bb2/" + } "" "${qc}" false \ + "expand multiple directory names" + + test_gdb_complete_multiple "file ${qc}${root}/" \ + "c" "c" { + "cc1/" + "cc2" + } "" "${qc}" false \ + "expand mixed directory and file names" + + # GDB does not currently escape word break characters + # (e.g. white space) correctly in unquoted filenames. + if { $qc ne "" } { + set sp " " + + test_gdb_complete_multiple "file ${qc}${root}/aaa/" \ + "a" "a${sp}" { + "aa bb" + "aa cc" + } "" "${qc}" false \ + "expand filenames containing spaces" + } + } } gdb_start diff --git a/gdb/top.c b/gdb/top.c index cf7d3a913ba..8df684ec8b4 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -2142,7 +2142,6 @@ init_main (void) rl_completion_word_break_hook = gdb_completion_word_break_characters; rl_attempted_completion_function = gdb_rl_attempted_completion_function; set_rl_completer_word_break_characters (default_word_break_characters ()); - rl_completer_quote_characters = get_gdb_completer_quote_characters (); rl_completion_display_matches_hook = cli_display_match_list; rl_readline_name = "gdb"; rl_terminal_name = getenv ("TERM");