From 8df899b1119cf960d39d848e7c2ea6b25e66fdd6 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Mon, 30 Mar 2026 21:01:20 -0700 Subject: [PATCH] utils: Protect CString routines against integer overflow heap corruption --- libs/utils/src/CString.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/utils/src/CString.cpp b/libs/utils/src/CString.cpp index c6bf648e26..ae88ee7114 100644 --- a/libs/utils/src/CString.cpp +++ b/libs/utils/src/CString.cpp @@ -19,12 +19,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include @@ -107,7 +109,9 @@ CString& CString::replace(size_type const pos, size_type len, char const* str, s len = std::min(len, size() - pos); - const size_type newSize = size() - len + l; + const size_type remainder = size() - len; + FILAMENT_CHECK_POSTCONDITION(l <= std::numeric_limits::max() - remainder); + const size_type newSize = remainder + l; // if the new string is not longer, we can do it in-place, which is much faster. if (newSize <= size()) {