diff --git a/include/fmt/format.h b/include/fmt/format.h index 06ecf557..76e4fac6 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -962,8 +962,9 @@ template struct null {}; // Workaround an array initialization issue in gcc 4.8. template struct fill_t { private: - enum { max_size = 5 }; - Char data_[max_size + 1]; + enum { max_size = 4 }; + Char data_[max_size]; + unsigned char size_; public: FMT_CONSTEXPR void operator=(basic_string_view s) { @@ -973,15 +974,10 @@ template struct fill_t { return; } for (size_t i = 0; i < size; ++i) data_[i] = s[i]; - data_[size] = Char(); - } - - size_t size() const { - size_t i = 1; - while (data_[i] && i <= max_size) ++i; - return i; + size_ = static_cast(size); } + size_t size() const { return size_; } const Char* data() const { return data_; } FMT_CONSTEXPR Char& operator[](size_t index) { return data_[index]; } @@ -992,6 +988,7 @@ template struct fill_t { static FMT_CONSTEXPR fill_t make() { auto fill = fill_t(); fill[0] = Char(' '); + fill.size_ = 1; return fill; } }; diff --git a/test/format-test.cc b/test/format-test.cc index 015a7438..8bce69be 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -816,6 +816,8 @@ TEST(FormatterTest, Fill) { EXPECT_EQ("foo=", format("{:}=", "foo")); EXPECT_EQ(std::string("\0\0\0*", 4), format(string_view("{:\0>4}", 6), '*')); EXPECT_EQ("жж42", format("{0:ж>4}", 42)); + EXPECT_THROW_MSG(format("{:\x80\x80\x80\x80\x80>}", 0), format_error, + "invalid fill"); } TEST(FormatterTest, PlusSign) {