mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-25 15:21:54 +00:00
Check for null string pointer.
This commit is contained in:
parent
f8c9106d67
commit
1b3c197bff
@ -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<size_t>(width, size));
|
||||
out = std::copy(str, str + size, out);
|
||||
if (static_cast<unsigned>(width) > size)
|
||||
|
@ -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<const char*>(0),
|
||||
FormatError, "string pointer is null");
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatPointer) {
|
||||
|
Loading…
Reference in New Issue
Block a user