public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Strip trailing newlines from input string
@ 2024-04-15 15:05 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2024-04-15 15:05 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bdcd50f901e3db5b773b6462813a50b9649aad57

commit bdcd50f901e3db5b773b6462813a50b9649aad57
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Apr 4 08:40:38 2024 -0600

    Strip trailing newlines from input string
    
    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 <legouguec@adacore.com>
    Approved-By: Luis Machado <luis.machado@arm.com>
    Tested-By: Luis Machado <luis.machado@arm.com>

Diff:
---
 gdb/event-top.c                          |  8 ++++++
 gdb/testsuite/gdb.base/paste-newline.exp | 45 ++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

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..dabceace665
--- /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 <http://www.gnu.org/licenses/>.
+
+# 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 ".*echo hello.*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 ".*echo hello.*hello.*\r\n.*$gdb_prompt $" {
+	    fail $gdb_test_name
+	}
+    }
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-04-15 15:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-15 15:05 [binutils-gdb] Strip trailing newlines from input string Tom Tromey

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).