Check for null string pointer.

This commit is contained in:
Victor Zverovich 2012-12-17 16:39:49 -08:00
parent f8c9106d67
commit 1b3c197bff
2 changed files with 10 additions and 2 deletions

View File

@ -408,8 +408,12 @@ void Formatter::DoFormat() {
ReportUnknownType(type, "string"); ReportUnknownType(type, "string");
const char *str = arg.string.value; const char *str = arg.string.value;
size_t size = arg.string.size; size_t size = arg.string.size;
if (size == 0 && *str) if (size == 0) {
if (!str)
throw FormatError("string pointer is null");
if (*str)
size = std::strlen(str); size = std::strlen(str);
}
char *out = GrowBuffer(std::max<size_t>(width, size)); char *out = GrowBuffer(std::max<size_t>(width, size));
out = std::copy(str, str + size, out); out = std::copy(str, str + size, out);
if (static_cast<unsigned>(width) > size) if (static_cast<unsigned>(width) > size)

View File

@ -609,6 +609,10 @@ TEST(FormatterTest, FormatCString) {
CheckUnknownTypes("test", "s", "string"); CheckUnknownTypes("test", "s", "string");
EXPECT_EQ("test", str(Format("{0}") << "test")); EXPECT_EQ("test", str(Format("{0}") << "test"));
EXPECT_EQ("test", str(Format("{0:s}") << "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<const char*>(0),
FormatError, "string pointer is null");
} }
TEST(FormatterTest, FormatPointer) { TEST(FormatterTest, FormatPointer) {