diff --git a/format.cc b/format.cc index 336d91b9..ee3224bc 100644 --- a/format.cc +++ b/format.cc @@ -514,6 +514,11 @@ class ArgFormatter : public BasicArgFormatter, Char> { template class PrintfArgFormatter : public BasicArgFormatter, Char> { + + void write_null_pointer() { + this->writer() << "(nil)"; + } + public: PrintfArgFormatter(BasicWriter &w, FormatSpec &s) : BasicArgFormatter, Char>(w, s) {} @@ -543,6 +548,8 @@ class PrintfArgFormatter : void visit_cstring(const char *value) { if (value) BasicArgFormatter, Char>::visit_cstring(value); + else if (this->spec().type_ == 'p') + write_null_pointer(); else this->writer() << "(null)"; } @@ -551,7 +558,7 @@ class PrintfArgFormatter : if (value) BasicArgFormatter, Char>::visit_pointer(value); else - this->writer() << "(nil)"; + write_null_pointer(); } void visit_custom(Arg::CustomValue c) { diff --git a/test/printf-test.cc b/test/printf-test.cc index f4faaf1c..c07384be 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -431,6 +431,8 @@ TEST(PrintfTest, Pointer) { EXPECT_PRINTF("(nil)", "%p", p); const char *s = "test"; EXPECT_PRINTF(fmt::format("{:p}", s), "%p", s); + const char *null_str = 0; + EXPECT_PRINTF("(nil)", "%p", null_str); } TEST(PrintfTest, Custom) {