From b8bd80ff28e4c0df0322f4ddd86c6455ab6835f1 Mon Sep 17 00:00:00 2001 From: vitaut Date: Wed, 25 Nov 2015 09:49:01 -0800 Subject: [PATCH] Fix handling of empty non-null-terminated strings --- format.cc | 4 +--- test/format-test.cc | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/format.cc b/format.cc index 6858058f..d17c2d68 100644 --- a/format.cc +++ b/format.cc @@ -432,7 +432,7 @@ class BasicArgFormatter : public ArgVisitor { } void write(const char *value) { - Arg::StringValue str = {value, 0}; + Arg::StringValue str = {value, value != 0 ? strlen(value) : 0}; writer_.write_str(str, spec_); } @@ -842,8 +842,6 @@ void fmt::BasicWriter::write_str( FMT_THROW(FormatError("string pointer is null")); return; } - if (*str_value) - str_size = std::char_traits::length(str_value); } std::size_t precision = spec.precision_; if (spec.precision_ >= 0 && precision < str_size) diff --git a/test/format-test.cc b/test/format-test.cc index 39195c83..d2b69921 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1649,3 +1649,12 @@ TEST(FormatTest, Enum) { EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum())); EXPECT_EQ("0", fmt::format("{}", A)); } + +struct EmptyTest {}; +std::ostream &operator<<(std::ostream &os, EmptyTest) { + return os << ""; +} + +TEST(FormatTest, EmptyCustomOutput) { + EXPECT_EQ("", fmt::format("{}", EmptyTest())); +}