Skip to content

Commit e4c65c1

Browse files
Fix String::compareTo(const char*) for invalid strings
When comparing an invalid String object to a non-empty char*, this would erronously return 0 (equal) because of a typo. This bug also masked three incorrect checks in related testcases. In two cases, a String was made invalid and then checked to still contain a value (these were changed to check that the string is invalid) and in one case the wrong string was checked.
1 parent be96485 commit e4c65c1

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

Diff for: api/String.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ int String::compareTo(const String &s) const
462462
int String::compareTo(const char *cstr) const
463463
{
464464
if (!buffer || !cstr) {
465-
if (cstr && !*cstr) return 0 - *(unsigned char *)cstr;
465+
if (cstr && *cstr) return 0 - *(unsigned char *)cstr;
466466
if (buffer && len > 0) return *(unsigned char *)buffer;
467467
return 0;
468468
}

Diff for: test/src/String/test_String.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ TEST_CASE ("Testing String(const __FlashStringHelper) constructor() with invalid
127127
char *buffer = NULL;
128128

129129
arduino::String str1(F(buffer));
130-
REQUIRE(str1.compareTo("Hello") == 0);
130+
REQUIRE_FALSE(str1);
131131
}
132132

133133
TEST_CASE ("Testing String(StringSumHelper &&) constructor()", "[String-Ctor-13]")
@@ -158,5 +158,5 @@ TEST_CASE ("Testing String(String &&) with move(String &rhs) from larger to smal
158158
arduino::String str("Hello");
159159
arduino::String str1("Arduino");
160160
str = static_cast<arduino::String&&>(str1);
161-
REQUIRE(str1.compareTo("Arduino") == 0);
161+
REQUIRE(str.compareTo("Arduino") == 0);
162162
}

Diff for: test/src/String/test_operators.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ TEST_CASE ("Testing & String::operator = (const char *)", "[String-operator+-14]
126126
char *buffer = NULL;
127127
arduino::String str("Hello");
128128
str = buffer;
129-
REQUIRE(str.compareTo("Hello") == 0);
129+
REQUIRE(str.compareTo("Hello") != 0);
130130
}
131131

132132
TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of first string", "[String-operator+-15]")

0 commit comments

Comments
 (0)