Don't truncate output size to int in fmt_vformat and vfprintf (#4987)

* Don't truncate output size to int in fmt_vformat and vfprintf

* Check size before writing in vfprintf
This commit is contained in:
aizu-m 2026-10-25 21:47:26 +05:30 committed by GitHub
parent 88a0ae2020
commit 3a2dcb59ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 0 deletions

View File

@ -630,6 +630,7 @@ auto vfprintf(std::FILE* f, basic_string_view<Char> fmt,
auto buf = basic_memory_buffer<Char>();
detail::vprintf(buf, fmt, args);
size_t size = buf.size();
if (size > detail::to_unsigned(detail::max_value<int>())) return -1;
return std::fwrite(buf.data(), sizeof(Char), size, f) < size
? -1
: static_cast<int>(size);

View File

@ -8,6 +8,7 @@
#include "fmt/fmt-c.h"
#include <fmt/core.h>
#include <limits.h> // 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<int>(num_args)));
if (result.size > INT_MAX) return fmt_error;
return static_cast<int>(result.size);
}
FMT_CATCH(...) {}