Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions include/spdlog/pattern_formatter-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class scoped_padder {

private:
void pad_it(long count) {
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)),
dest_);
// never read past spaces_ (handle_padspec_ caps the width at its size)
const size_t n =
count > 0 ? std::min<size_t>(static_cast<size_t>(count), spaces_.size()) : 0;
fmt_helper::append_string_view(string_view_t(spaces_.data(), n), dest_);
}

const padding_info &padinfo_;
Expand Down Expand Up @@ -1300,8 +1302,11 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(

auto width = static_cast<size_t>(*it) - '0';
for (++it; it != end && std::isdigit(static_cast<unsigned char>(*it)); ++it) {
auto digit = static_cast<size_t>(*it) - '0';
width = width * 10 + digit;
// stop accumulating once above max_width, so long digit runs can't wrap around
if (width <= max_width) {
auto digit = static_cast<size_t>(*it) - '0';
width = width * 10 + digit;
}
}

// search for the optional truncate marker '!'
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pattern_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ TEST_CASE("left_padded_huge", "[pattern_formatter]") {

REQUIRE(log_to_str("Some message", "[%-300!n] %v", spdlog::pattern_time_type::local, "\n") ==
"[pattern_tester ] Some message\n");

// width digits that would overflow size_t are still capped
REQUIRE(log_to_str("Some message", "[%-18446744073709551617n] %v",
spdlog::pattern_time_type::local, "\n") ==
"[pattern_tester ] Some message\n");
}

TEST_CASE("left_padded_max", "[pattern_formatter]") {
Expand Down