[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.
This commit is contained in:
Tom de Vries 2026-01-06 22:44:31 +01:00
parent 6a275e7ffe
commit f94da18382

View 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;
}
}