From 35dcc58263d6b55419a5932bd6b0b3029a0a8c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20P=C3=B6schko?= Date: Sun, 20 Jul 2025 19:43:33 +0200 Subject: [PATCH] fix buffer overflow on all emphasis flags set (#4498) --- include/fmt/color.h | 2 +- test/color-test.cc | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index 638f15b4..e0c7665f 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -430,7 +430,7 @@ template struct ansi_color_escape { private: static constexpr size_t num_emphases = 8; - Char buffer[7u + 3u * num_emphases + 1u]; + Char buffer[7u + 4u * num_emphases + 1u]; static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out, char delimiter) noexcept { diff --git a/test/color-test.cc b/test/color-test.cc index e295ab5e..86faca9d 100644 --- a/test/color-test.cc +++ b/test/color-test.cc @@ -113,6 +113,15 @@ TEST(color_test, format) { EXPECT_EQ(fmt::format("{}", fmt::styled("bar", fg(fmt::color::blue) | fmt::emphasis::underline)), "\x1b[4m\x1b[38;2;000;000;255mbar\x1b[0m"); + EXPECT_EQ( + fmt::format( + "{}", fmt::styled( + "all", fmt::emphasis::bold | fmt::emphasis::faint | + fmt::emphasis::italic | + fmt::emphasis::underline | fmt::emphasis::blink | + fmt::emphasis::reverse | fmt::emphasis::conceal | + fmt::emphasis::strikethrough)), + "\x1b[1m\x1b[2m\x1b[3m\x1b[4m\x1b[5m\x1b[7m\x1b[8m\x1b[9mall\x1b[0m"); } TEST(color_test, format_to) {