From a906e0c857c8b5fed254bf7a7ba1280f4a39f6cd Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 17 Oct 2021 16:01:47 +0200 Subject: [PATCH] Add assertions to String index operator to prevent invalid memory access --- Source/Engine/Core/Types/String.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Core/Types/String.h b/Source/Engine/Core/Types/String.h index 542d2f9b3..9b053f8b5 100644 --- a/Source/Engine/Core/Types/String.h +++ b/Source/Engine/Core/Types/String.h @@ -48,6 +48,7 @@ public: /// The character FORCE_INLINE T& operator[](int32 index) { + ASSERT(index >= 0 && index < _length); return _data[index]; } @@ -58,6 +59,7 @@ public: /// The character FORCE_INLINE const T& operator[](int32 index) const { + ASSERT(index >= 0 && index < _length); return _data[index]; } @@ -441,15 +443,9 @@ public: /// Number of replacements made (in other words number of occurences of searchText). int32 Replace(const T* searchText, int32 searchTextLength, const T* replacementText, int32 replacementTextLength, StringSearchCase searchCase = StringSearchCase::CaseSensitive) { - if (!HasChars()) + if (!HasChars() || searchTextLength == 0) return 0; - if (searchTextLength == 0) - { - ASSERT(false); // Empty search text never makes sense, and is always sign of a bug in calling code. - return 0; - } - int32 replacedCount = 0; if (searchTextLength == replacementTextLength)