Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Make 'printf' converter output "(null)" instead of "null" #85845

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/docs/dev/printf_behavior.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _printf_behavior:

====================================
Printf Behavior Under All Conditions
====================================
Expand Down
7 changes: 7 additions & 0 deletions libc/docs/dev/undefined_behavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ The POSIX.1 standard does not delineate the behavior consequent to invoking hsea
Path without Leading Slashs in shm_open
----------------------------------------
POSIX.1 leaves that when the name of a shared memory object does not begin with a slash, the behavior is implementation defined. In such cases, the shm_open in LLVM libc is implemented to behave as if the name began with a slash.

Handling of NULL arguments to the 's' format specifier
------------------------------------------------------
The C standard does not specify behavior for ``printf("%s", NULL)``. We will
print the string literal ``(null)`` unless using the
``LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS`` option described in :ref:`printf
behavior<printf_behavior>`.
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/string_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LIBC_INLINE int convert_string(Writer *writer, const FormatSection &to_conv) {

#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
if (str_ptr == nullptr) {
str_ptr = "null";
str_ptr = "(null)";
}
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS

Expand Down
4 changes: 2 additions & 2 deletions libc/test/src/stdio/sprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ TEST(LlvmLibcSPrintfTest, StringConv) {

#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
written = LIBC_NAMESPACE::sprintf(buff, "%s", nullptr);
EXPECT_EQ(written, 4);
ASSERT_STREQ(buff, "null");
EXPECT_EQ(written, 6);
ASSERT_STREQ(buff, "(null)");
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
}

Expand Down
Loading