From b0d9e81d85a7a884b60a9fd0e52fb6f8d2124a29 Mon Sep 17 00:00:00 2001 From: hayt Date: Sun, 22 Sep 2024 10:15:47 +0200 Subject: [PATCH] fix: prevent size_t-max length string allocation prevent an issue, where a string was created with size_t(0)-1 length. When creating underlines for error output, the current column sometimes had cases where it was set to 0 caused an underflow. --- include/toml11/impl/source_location_impl.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/toml11/impl/source_location_impl.hpp b/include/toml11/impl/source_location_impl.hpp index f7f4638..7a2fc8c 100644 --- a/include/toml11/impl/source_location_impl.hpp +++ b/include/toml11/impl/source_location_impl.hpp @@ -116,7 +116,9 @@ TOML11_INLINE std::ostringstream& format_underline(std::ostringstream& oss, oss << make_string(lnw + 1, ' ') << color::bold << color::blue << " | " << color::reset; - oss << make_string(col-1 /*1-origin*/, ' ') + // in case col is 0, so we don't create a string with size_t max length + const std::size_t sanitized_col = col == 0 ? 0 : col - 1 /*1-origin*/; + oss << make_string(sanitized_col, ' ') << color::bold << color::red << make_string(len, '^') << "-- " << color::reset << msg << '\n';