Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect string comparisons against segfaults #547

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/EASTL/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,8 @@ namespace eastl
template <typename T, typename Allocator>
inline bool operator==(const typename basic_string<T, Allocator>::value_type* p, const basic_string<T, Allocator>& b)
{
if (p == nullptr)
return false;
typedef typename basic_string<T, Allocator>::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));
Expand All @@ -3551,6 +3553,8 @@ namespace eastl
template <typename T, typename Allocator>
inline bool operator==(const basic_string<T, Allocator>& a, const typename basic_string<T, Allocator>::value_type* p)
{
if (p == nullptr)
return false;
typedef typename basic_string<T, Allocator>::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));
Expand Down
28 changes: 28 additions & 0 deletions test/source/TestString.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down