From f01641e46325261d7803de820632fe85fb6e135c Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 6 May 2026 17:28:39 -0400 Subject: [PATCH] overflow patch. --- .../generic/builder/json_string_builder-inl.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/simdjson/generic/builder/json_string_builder-inl.h b/include/simdjson/generic/builder/json_string_builder-inl.h index 0f7707851..c77f118d4 100644 --- a/include/simdjson/generic/builder/json_string_builder-inl.h +++ b/include/simdjson/generic/builder/json_string_builder-inl.h @@ -1,5 +1,6 @@ #include #include +#include #include #ifndef SIMDJSON_GENERIC_STRING_BUILDER_INL_H @@ -755,6 +756,11 @@ simdjson_inline void string_builder::append(number_type v) noexcept { simdjson_inline void string_builder::escape_and_append(std::string_view input) noexcept { // escaping might turn a control character into \x00xx so 6 characters. + // Guard against size_t overflow in the multiplication below. + if (input.size() > (std::numeric_limits::max)() / 6) { + set_valid(false); + return; + } if (capacity_check(6 * input.size())) { position += write_string_escaped(input, buffer.get() + position); } @@ -763,6 +769,11 @@ string_builder::escape_and_append(std::string_view input) noexcept { simdjson_inline void string_builder::escape_and_append_with_quotes(std::string_view input) noexcept { // escaping might turn a control character into \x00xx so 6 characters. + // Guard against size_t overflow in the arithmetic below. + if (input.size() > ((std::numeric_limits::max)() - 2) / 6) { + set_valid(false); + return; + } if (capacity_check(2 + 6 * input.size())) { buffer.get()[position++] = '"'; position += write_string_escaped(input, buffer.get() + position);