From: Tom de Vries Date: Tue, 6 Jan 2026 21:44:31 +0000 (+0100) Subject: [gdb] Fix confusing string in command_line_append_input_line X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f94da18382ab6507ec556489a266502cfe0f042d;p=thirdparty%2Fbinutils-gdb.git [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 Tested on x86_64-linux. --- 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; } }