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