From 756132aedcf0da8bd4b37d1a63743a50a4e37f65 Mon Sep 17 00:00:00 2001 From: Joschka Seydell Date: Mon, 2 Dec 2024 21:50:53 +0100 Subject: [PATCH 1/2] string/test: Add test cases for global comparison operators. --- test/source/TestString.inl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/source/TestString.inl b/test/source/TestString.inl index 193a9291..db29079c 100644 --- a/test/source/TestString.inl +++ b/test/source/TestString.inl @@ -2155,6 +2155,34 @@ int TEST_STRING_NAME() VERIFY(LocalHash(sw2) == LocalHash(sw3)); } + // test == and != operators + { + StringType str1(LITERAL("abcdefghijklmnopqrstuvwxyz")); + StringType str2(LITERAL("abcdefghijklmnopqrstuvwxyz")); + StringType str3(LITERAL("Hello, world")); + typename StringType::value_type *nullChar{nullptr}; + { + + VERIFY(!(str1 == nullptr)); + VERIFY(!(nullptr == str1)); + VERIFY(!(str1 == nullChar)); + VERIFY(!(nullChar == str1)); + + VERIFY(str1 != nullptr); + VERIFY(nullptr != str1); + VERIFY(str1 != nullChar); + VERIFY(nullChar != str1); + } + { + VERIFY(str1 == str2); + VERIFY(str1 != str3); + VERIFY(str1 == str2.c_str()); + VERIFY(str1 != str3.c_str()); + VERIFY(str1.c_str() == str2); + VERIFY(str1.c_str() != str3); + } + } + // test <=> operator #if defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON) { From 85995a464a85b53db941340b08e08a3348067039 Mon Sep 17 00:00:00 2001 From: Joschka Seydell Date: Mon, 2 Dec 2024 21:58:09 +0100 Subject: [PATCH 2/2] string: Prevent segfaults in global equality comparators. --- include/EASTL/string.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/EASTL/string.h b/include/EASTL/string.h index 5f8d7d09..435a049c 100644 --- a/include/EASTL/string.h +++ b/include/EASTL/string.h @@ -3542,6 +3542,8 @@ namespace eastl template inline bool operator==(const typename basic_string::value_type* p, const basic_string& b) { + if (p == nullptr) + return false; typedef typename basic_string::size_type string_size_type; const string_size_type n = (string_size_type)CharStrlen(p); return ((n == b.size()) && (Compare(p, b.data(), (size_t)n) == 0)); @@ -3551,6 +3553,8 @@ namespace eastl template inline bool operator==(const basic_string& a, const typename basic_string::value_type* p) { + if (p == nullptr) + return false; typedef typename basic_string::size_type string_size_type; const string_size_type n = (string_size_type)CharStrlen(p); return ((a.size() == n) && (Compare(a.data(), p, (size_t)n) == 0));