[binutils-gdb] [gdb] Fix confusing string in command_line_append_input_line

Tom de Vries vries@sourceware.org
Tue Jan 6 21:44:35 GMT 2026


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

commit f94da18382ab6507ec556489a266502cfe0f042d
Author: Tom de Vries <tdevries@suse.de>
Date:   Tue Jan 6 22:44:31 2026 +0100

    [gdb] Fix confusing string in command_line_append_input_line
    
    While writing a unit test for PR33754, I ran into an std::string s where
    where strlen (s.data ()) != s.size ().
    
    I tracked this down to command_line_append_input_line, where we do:
    ...
          /* Copy whole line including terminating null, and we're
             done.  */
          cmd_line_buffer.append (rl, len + 1);
    ...
    
    As example, consider string s:
    ...
    std::string s = "";
    s.append ("", 1);
    ...
    
    Initially, the string is empty, and we have:
    - strlen (s.data ()) == 0
    - s.size () == 0
    
    After appending '\0', we have:
    - strlen (s.data ()) == 0
    - s.size () == 1
    
    While I suppose this is legal, I think it's better to avoid this type of
    string, since it tends to cause confusion and off-by-one errors.
    
    And AFAIU, in this case the '\0' is not necessary, it's a remnant of using C
    strings.
    
    Fix this by simply appending rl.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    Tested on x86_64-linux.

Diff:
---
 gdb/event-top.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 005ea76ff5c..d27ce02e61c 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -633,9 +633,8 @@ command_line_append_input_line (std::string &cmd_line_buffer, const char *rl)
     }
   else
     {
-      /* Copy whole line including terminating null, and we're
-	 done.  */
-      cmd_line_buffer.append (rl, len + 1);
+      /* Copy whole line, and we're done.  */
+      cmd_line_buffer.append (rl);
       return true;
     }
 }


More information about the Gdb-cvs mailing list