This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Print embedded nuls in char arrays (PR44649)
ClosedPublic

Authored by labath on Oct 12 2021, 5:42 AM.

Details

Summary

When we know the bounds of the array, print any embedded nuls instead of
treating them as terminators. An exception to this rule is made for the
nul character at the very end of the string. We don't print that, as
otherwise 99% of the strings would end in \0. This way the strings
usually come out the same as how the user typed it into the compiler
(char foo[] = "with\0nuls"). It also matches how they come out in gdb.

This resolves a FIXME left from D111399, and leaves another FIXME for dealing
with nul characters in "escape-non-printables=false" mode. In this mode the
characters cause the entire summary string to be terminated prematurely.

Diff Detail

Event Timeline

labath requested review of this revision.Oct 12 2021, 5:42 AM
labath created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2021, 5:42 AM
teemperor accepted this revision.Oct 13 2021, 7:44 AM

LGTM

lldb/test/API/functionalities/data-formatter/stringprinter/main.cpp
41

I think the whole 'empty lines break inline tests' thing has been fixed at some point, so could you drop a new line between this and the printable thing when you land?

This revision is now accepted and ready to land.Oct 13 2021, 7:44 AM
labath added inline comments.Oct 14 2021, 12:00 AM
lldb/test/API/functionalities/data-formatter/stringprinter/main.cpp
41

sounds good

This revision was automatically updated to reflect the committed changes.

An exception to this rule is made for the nul character at the very end of the string.

@labath What do you think of hiding all trailing nul characters, not just the final one? For example consider:

char buf[16] = "hello world";
# v buf
# (char[16]) buf = "hello world\0\0\0\0"
  1. This shows 4 out of 5 nuls, which strikes me as odd
  2. None of the nuls hide any further data, so it seems friendly enough to print up to the last nul

thanks

Yeah, I suppose that makes sense. Like, I don't think we can ever have a single solution that does exactly what people would expect in every possible situation. The only counterargument I can think of is "but gdb does it that way", but I don't think that's a particularly strong one in this case.