Test invalid fill

This commit is contained in:
Victor Zverovich 2020-01-19 15:57:11 -08:00
parent 8a3a8177d6
commit b4218aa0f8
2 changed files with 8 additions and 9 deletions

View File

@ -962,8 +962,9 @@ template <typename T = void> struct null {};
// Workaround an array initialization issue in gcc 4.8. // Workaround an array initialization issue in gcc 4.8.
template <typename Char> struct fill_t { template <typename Char> struct fill_t {
private: private:
enum { max_size = 5 }; enum { max_size = 4 };
Char data_[max_size + 1]; Char data_[max_size];
unsigned char size_;
public: public:
FMT_CONSTEXPR void operator=(basic_string_view<Char> s) { FMT_CONSTEXPR void operator=(basic_string_view<Char> s) {
@ -973,15 +974,10 @@ template <typename Char> struct fill_t {
return; return;
} }
for (size_t i = 0; i < size; ++i) data_[i] = s[i]; for (size_t i = 0; i < size; ++i) data_[i] = s[i];
data_[size] = Char(); size_ = static_cast<unsigned char>(size);
}
size_t size() const {
size_t i = 1;
while (data_[i] && i <= max_size) ++i;
return i;
} }
size_t size() const { return size_; }
const Char* data() const { return data_; } const Char* data() const { return data_; }
FMT_CONSTEXPR Char& operator[](size_t index) { return data_[index]; } FMT_CONSTEXPR Char& operator[](size_t index) { return data_[index]; }
@ -992,6 +988,7 @@ template <typename Char> struct fill_t {
static FMT_CONSTEXPR fill_t<Char> make() { static FMT_CONSTEXPR fill_t<Char> make() {
auto fill = fill_t<Char>(); auto fill = fill_t<Char>();
fill[0] = Char(' '); fill[0] = Char(' ');
fill.size_ = 1;
return fill; return fill;
} }
}; };

View File

@ -816,6 +816,8 @@ TEST(FormatterTest, Fill) {
EXPECT_EQ("foo=", format("{:}=", "foo")); EXPECT_EQ("foo=", format("{:}=", "foo"));
EXPECT_EQ(std::string("\0\0\0*", 4), format(string_view("{:\0>4}", 6), '*')); EXPECT_EQ(std::string("\0\0\0*", 4), format(string_view("{:\0>4}", 6), '*'));
EXPECT_EQ("жж42", format("{0:ж>4}", 42)); EXPECT_EQ("жж42", format("{0:ж>4}", 42));
EXPECT_THROW_MSG(format("{:\x80\x80\x80\x80\x80>}", 0), format_error,
"invalid fill");
} }
TEST(FormatterTest, PlusSign) { TEST(FormatterTest, PlusSign) {