Skip to content

Commit

Permalink
[libc] Make 'printf' converter output "(null)" instead of "null"
Browse files Browse the repository at this point in the history
Summary:
Currently we print `null` for the null pointer in a `%s` expression.
Although it's not defined by the standard, other implementations choose
to use `(null)` to indicate this. We also currently print `(nullptr)` so
I think it's more consistent to use parens in both cases.
  • Loading branch information
jhuber6 committed Mar 19, 2024
1 parent d4fb50d commit 5c8d1b4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
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

0 comments on commit 5c8d1b4

Please sign in to comment.