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

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Mar 19, 2024

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 19, 2024

@llvm/pr-subscribers-libc

Author: Joseph Huber (jhuber6)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/85845.diff

2 Files Affected:

  • (modified) libc/src/stdio/printf_core/string_converter.h (+1-1)
  • (modified) libc/test/src/stdio/sprintf_test.cpp (+2-2)
diff --git a/libc/src/stdio/printf_core/string_converter.h b/libc/src/stdio/printf_core/string_converter.h
index 04dc5a06da2226..9e05591079faa4 100644
--- a/libc/src/stdio/printf_core/string_converter.h
+++ b/libc/src/stdio/printf_core/string_converter.h
@@ -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
 
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index ab8bdb23b1187c..8dde95d02a96dc 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -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
 }
 

@SchrodingerZhu
Copy link
Contributor

SchrodingerZhu commented Mar 19, 2024

Please add design decisions to the undefined behavior documentation.

https://github.com/llvm/llvm-project/blob/main/libc/docs/dev/undefined_behavior.rst

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.
Copy link
Contributor

@SchrodingerZhu SchrodingerZhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jhuber6 jhuber6 merged commit 3dc1b50 into llvm:main Mar 19, 2024
4 of 5 checks passed
@michaelrj-google
Copy link
Contributor

This patch LGTM, thank you.

chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
…m#85845)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants