From patchwork Mon Apr 8 17:18:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88187 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 995DB3858424 for ; Mon, 8 Apr 2024 17:18:57 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id D5FDC3858D28 for ; Mon, 8 Apr 2024 17:18:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5FDC3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D5FDC3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712596709; cv=none; b=N793RXLHFOb+aX1TAne+nwWtc6PW+xhuFz9G+NfXcB4u5lFnMoZqSgOvZJ6dwiUWCrRV6F12Pszs9UHt3nvEFa/MiRL1TkR2RUOMugCg4jKSjm+Hqzhc7AxVvvODBloJSqtu0rBa5TQ+Ngw0asOe/Kfixt2ujB559vzGTHoNVXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712596709; c=relaxed/simple; bh=H6eOBgV8NX1x+kvQQ1tx+r5qKf0i7YdePdh2znpS2JQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wBBM1mQBZHEQqtyKX9/xFKGuBJer2UivaTj90JLlofh3nA1omC0AINEZsHTBUNayAp5xA8UORgoar5+AAcu31s+/hJvPfEbYHYpDEOrDd3VcpPLuNQFvNlW9y5tSf8w7inlJMEq70i09bLDxPZCVKvhg7Hxl960gnBwagq9SAz4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-7d03a66e895so158689739f.1 for ; Mon, 08 Apr 2024 10:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1712596706; x=1713201506; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VdjbeZi4BsMAwjRF6jCXo+saJMAXj7lcFNK2zn5olh0=; b=S4heXKi4gj2Ep/6Ej3vsvCd9vJ2xAHecOL+Umm83NxEwkeadxuA1VzNRK6+R6wZs74 SAOzkYNs7vkKGxXHk8BCB5rEzlv/OlVCRREvvYbNnojneE+OFHAwW4vrh/BBTBI5dkeS sXfQBMLy725208eEVMVozWJ+zA+CXcNjpw3v2SbKgiumelMmQ4zYLFgWTU8GW9qnv4YI h94KvJXzB+URFyYDRYkv5o956lwNILM1rxUTQK4jAJv0kBT8MMcvkmKtlUdd3ytI7d2k 7aylH5FVTQHF+BYgsv6y7t5TPntaGFmO27XhodrmvaDcn9YKQUzEwHWTNDL5KfpSFhDN 7e2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712596706; x=1713201506; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VdjbeZi4BsMAwjRF6jCXo+saJMAXj7lcFNK2zn5olh0=; b=eioykpwuC1QZxIV0OeXJM3h484phMcxXgB6BLPgQQERyu/EqCQqBdxhwWl5VbeQYOd 8HccYH2x4If+ff/Kg6ENDfntU5MjSIoKAd/jhicrmHPP/4PxSIVZhAZH9ZBBmUoChUI+ gex+aBT+dA5bexMWki4+CCpDyV2/+2EsISIx/rRUl091+Aesg2ARdNAOYhcLQiMyr/iB 61zI1umMTIRc1j923Qg0wdfqIH33wDABCSmD8P/VypxAV37I2xL2VqlPqxUzENfrDyUQ KwAk2kk5EdTN2dOlO4yqPf/cVkDmSr1K5Uoy/OxQt+jzP2gdXa9v8lpwN0Po0ejWUeGK Brbg== X-Gm-Message-State: AOJu0YzUlEE4BWot9s+JO2rrbAY9BIcpWEyrzEnrtGLVhSoBOrFbH2g0 6vJ+kJIc+JKsLjSACac11XV5VoxcL1Y3iI/gExdrAj04zDEgqbb9fOsxSTt3ZjNRBX1l9l2/9YQ = X-Google-Smtp-Source: AGHT+IGzWWUZXVpJMX+fNr52cXtqsBK9zuGjrC685Yj6Y3O9LmL328+O1rV6NJmKwMguHXA+LCU6rA== X-Received: by 2002:a92:911:0:b0:36a:12d4:a5b3 with SMTP id y17-20020a920911000000b0036a12d4a5b3mr311826ilg.8.1712596705887; Mon, 08 Apr 2024 10:18:25 -0700 (PDT) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id bv24-20020a056638449800b0047be26df380sm2687497jab.2.2024.04.08.10.18.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 10:18:25 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey , =?utf-8?q?K=C3=A9vin_Le_Gouguec?= Subject: [PATCH] Strip trailing newlines from input string Date: Mon, 8 Apr 2024 11:18:18 -0600 Message-ID: <20240408171818.1856529-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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 A co-worker noticed a strange situation where "target remote" would fail due to a trailing newline in the address part of the command. Eventually he tracked this down to the fact that he was pasting the command into the terminal, and due to bracketed paste mode, the newline was being preserved by readline. It seems to me that we basically never want a trailing newline on a gdb command, so this patch removes it when handling the readline result. Co-Authored-By: Kévin Le Gouguec Tested-By: Luis Machado --- gdb/event-top.c | 8 +++++ gdb/testsuite/gdb.base/paste-newline.exp | 45 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 gdb/testsuite/gdb.base/paste-newline.exp diff --git a/gdb/event-top.c b/gdb/event-top.c index 9a02ac6df27..f0c07ba7f64 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -251,6 +251,14 @@ gdb_rl_callback_handler (char *rl) noexcept static struct gdb_exception gdb_rl_expt; struct ui *ui = current_ui; + /* In bracketed paste mode, pasting a complete line can result in a + literal newline appearing at the end of LINE. However, we never + want this in gdb. */ + size_t len = strlen (rl); + while (len > 0 && (rl[len - 1] == '\r' || rl[len - 1] == '\n')) + --len; + rl[len] = '\0'; + try { /* Ensure the exception is reset on each call. */ diff --git a/gdb/testsuite/gdb.base/paste-newline.exp b/gdb/testsuite/gdb.base/paste-newline.exp new file mode 100644 index 00000000000..b886fae6871 --- /dev/null +++ b/gdb/testsuite/gdb.base/paste-newline.exp @@ -0,0 +1,45 @@ +# Copyright (C) 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This test script checks that a trailing newline is stripped from a +# bracketed paste. + +save_vars { env(TERM) env(INPUTRC) } { + setenv TERM ansi + + # Create an inputrc file that enables bracketed paste mode. + set inputrc [standard_output_file inputrc] + set fd [open "$inputrc" w] + puts $fd "set enable-bracketed-paste on" + close $fd + + setenv INPUTRC "$inputrc" + + clean_restart + + send_gdb "\033\[200~echo hello\n\033\[201~\n" + + gdb_test_multiple "" "newline removed from paste" { + -re "hello\[^\n\]*$gdb_prompt $" { + # Some escape sequences are expected between echo's output + # and the prompt (e.g. the paste-bracketing toggle + # sequences) but _newlines_ are not. + pass $gdb_test_name + } + -re "hello.*\r\n.*$gdb_prompt $" { + fail $gdb_test_name + } + } +}