]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb] Fix confusing string in command_line_append_input_line
authorTom de Vries <tdevries@suse.de>
Tue, 6 Jan 2026 21:44:31 +0000 (22:44 +0100)
committerTom de Vries <tdevries@suse.de>
Tue, 6 Jan 2026 21:44:31 +0000 (22:44 +0100)
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.

gdb/event-top.c

index 005ea76ff5c5693dfa8ece7acfb16569c2b616e0..d27ce02e61cbd217d8eae1966ff0260d9cc2d32e 100644 (file)
@@ -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;
     }
 }