This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add nullptr check option to printf %s
ClosedPublic

Authored by michaelrj on Aug 2 2023, 10:55 AM.

Details

Summary

Some printf implementations perform a null check on pointers passed to
%s. While that's not in the standard, this patch adds it as an option
for compatibility. It also puts a similar check in %n behind the same
flag.

Diff Detail

Event Timeline

michaelrj created this revision.Aug 2 2023, 10:55 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 2 2023, 10:55 AM
michaelrj requested review of this revision.Aug 2 2023, 10:55 AM
lntue accepted this revision.Aug 2 2023, 11:54 AM
lntue added inline comments.
libc/test/src/stdio/sprintf_test.cpp
101

Also add the expectation for this output when LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS is set.

2791

Also add the expectation for this output when LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS is set.

This revision is now accepted and ready to land.Aug 2 2023, 11:54 AM
michaelrj added inline comments.Aug 2 2023, 12:54 PM
libc/test/src/stdio/sprintf_test.cpp
101

If the flag isn't set, the expectation is for it to segfault so it would have to be a death test. I'm not sure that's a useful test though.

This revision was automatically updated to reflect the committed changes.
mcgrathr added inline comments.
libc/src/stdio/printf_core/string_converter.h
29

IIRC the traditional string is "(null)", not "null".
That's what glibc does.
Also note for %p it uses "(nil)" instead, so go figure.
Also glibc only writes "(null)" at all if the precision allows the full string, never truncates it to "(nu" for %.3s. If it doesn't fit, it just writes nothing instead (as for an argument of "").