Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a675d5a

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 f7311d9 commit a675d5a

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

‎api/String.cpp‎

Lines changed: 1 addition & 1 deletion
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
}

‎test/src/String/test_String.cpp‎

Lines changed: 2 additions & 2 deletions
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
}

‎test/src/String/test_operators.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,20 @@ TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of
121121
REQUIRE(str1.compareTo(str2) == 0);
122122
}
123123

124-
TEST_CASE ("Testing & String::operator = (const char *)", "[String-operator+-14]")
124+
TEST_CASE ("Testing & String::operator = (const char *) with NULL does not leave string unchanged", "[String-operator+-14]")
125125
{
126126
char *buffer = NULL;
127127
arduino::String str("Hello");
128128
str = buffer;
129-
REQUIRE(str.compareTo("Hello") == 0);
129+
REQUIRE(str.compareTo("Hello") != 0);
130+
}
131+
132+
TEST_CASE ("Testing & String::operator = (const char *) with NULL produces invalid string", "[String-operator+-14]")
133+
{
134+
char *buffer = NULL;
135+
arduino::String str("Hello");
136+
str = buffer;
137+
REQUIRE_FALSE(str);
130138
}
131139

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /