diff --git a/format.cc b/format.cc index bb486da6..a4ab143d 100644 --- a/format.cc +++ b/format.cc @@ -408,8 +408,12 @@ void Formatter::DoFormat() { ReportUnknownType(type, "string"); const char *str = arg.string.value; size_t size = arg.string.size; - if (size == 0 && *str) - size = std::strlen(str); + if (size == 0) { + if (!str) + throw FormatError("string pointer is null"); + if (*str) + size = std::strlen(str); + } char *out = GrowBuffer(std::max(width, size)); out = std::copy(str, str + size, out); if (static_cast(width) > size) diff --git a/format_test.cc b/format_test.cc index 1cdeb619..3f6d456b 100644 --- a/format_test.cc +++ b/format_test.cc @@ -609,6 +609,10 @@ TEST(FormatterTest, FormatCString) { CheckUnknownTypes("test", "s", "string"); EXPECT_EQ("test", str(Format("{0}") << "test")); EXPECT_EQ("test", str(Format("{0:s}") << "test")); + char nonconst[] = "nonconst"; + EXPECT_EQ("nonconst", str(Format("{0}") << nonconst)); + EXPECT_THROW_MSG(Format("{0}") << reinterpret_cast(0), + FormatError, "string pointer is null"); } TEST(FormatterTest, FormatPointer) {