diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 03c8746d..a7213d01 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -630,6 +630,7 @@ auto vfprintf(std::FILE* f, basic_string_view fmt, auto buf = basic_memory_buffer(); detail::vprintf(buf, fmt, args); size_t size = buf.size(); + if (size > detail::to_unsigned(detail::max_value())) return -1; return std::fwrite(buf.data(), sizeof(Char), size, f) < size ? -1 : static_cast(size); diff --git a/src/fmt-c.cc b/src/fmt-c.cc index aec19072..5014d694 100644 --- a/src/fmt-c.cc +++ b/src/fmt-c.cc @@ -8,6 +8,7 @@ #include "fmt/fmt-c.h" #include +#include // INT_MAX constexpr size_t max_c_format_args = 16; @@ -45,6 +46,7 @@ extern "C" int fmt_vformat(char* buffer, size_t size, const char* fmt, auto result = fmt::vformat_to_n( buffer, size, fmt, fmt::format_args(format_args, static_cast(num_args))); + if (result.size > INT_MAX) return fmt_error; return static_cast(result.size); } FMT_CATCH(...) {}