mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-06 23:30:29 +00:00
Fix an overflow in format_to_n (#2029)
This commit is contained in:
parent
6cdd1be93e
commit
2c734c9bca
@ -766,7 +766,7 @@ class fixed_buffer_traits {
|
|||||||
explicit fixed_buffer_traits(size_t limit) : limit_(limit) {}
|
explicit fixed_buffer_traits(size_t limit) : limit_(limit) {}
|
||||||
size_t count() const { return count_; }
|
size_t count() const { return count_; }
|
||||||
size_t limit(size_t size) {
|
size_t limit(size_t size) {
|
||||||
size_t n = limit_ - count_;
|
size_t n = limit_ > count_ ? limit_ - count_ : 0;
|
||||||
count_ += size;
|
count_ += size;
|
||||||
return size < n ? size : n;
|
return size < n ? size : n;
|
||||||
}
|
}
|
||||||
|
@ -2031,6 +2031,11 @@ TEST(FormatTest, FormatToN) {
|
|||||||
result = fmt::format_to_n(buffer, 4, "{}", "ABCDE");
|
result = fmt::format_to_n(buffer, 4, "{}", "ABCDE");
|
||||||
EXPECT_EQ(5u, result.size);
|
EXPECT_EQ(5u, result.size);
|
||||||
EXPECT_EQ("ABCD", fmt::string_view(buffer, 4));
|
EXPECT_EQ("ABCD", fmt::string_view(buffer, 4));
|
||||||
|
|
||||||
|
buffer[3] = 'x';
|
||||||
|
result = fmt::format_to_n(buffer, 3, "{}", std::string(1000, '*'));
|
||||||
|
EXPECT_EQ(1000u, result.size);
|
||||||
|
EXPECT_EQ("***x", fmt::string_view(buffer, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatTest, WideFormatToN) {
|
TEST(FormatTest, WideFormatToN) {
|
||||||
|
Loading…
Reference in New Issue
Block a user