diff --git a/api/String.cpp b/api/String.cpp index 37812418..6f14ae13 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -462,7 +462,7 @@ int String::compareTo(const String &s) const int String::compareTo(const char *cstr) const { if (!buffer || !cstr) { - if (cstr && !*cstr) return 0 - *(unsigned char *)cstr; + if (cstr && *cstr) return 0 - *(unsigned char *)cstr; if (buffer && len > 0) return *(unsigned char *)buffer; return 0; } diff --git a/test/src/String/test_String.cpp b/test/src/String/test_String.cpp index ac3fdea2..95e0c902 100644 --- a/test/src/String/test_String.cpp +++ b/test/src/String/test_String.cpp @@ -127,7 +127,7 @@ TEST_CASE ("Testing String(const __FlashStringHelper) constructor() with invalid char *buffer = NULL; arduino::String str1(F(buffer)); - REQUIRE(str1.compareTo("Hello") == 0); + REQUIRE_FALSE(str1); } 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 arduino::String str("Hello"); arduino::String str1("Arduino"); str = static_cast(str1); - REQUIRE(str1.compareTo("Arduino") == 0); + REQUIRE(str.compareTo("Arduino") == 0); } diff --git a/test/src/String/test_operators.cpp b/test/src/String/test_operators.cpp index 087f7dc5..b71638d0 100644 --- a/test/src/String/test_operators.cpp +++ b/test/src/String/test_operators.cpp @@ -121,12 +121,20 @@ TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of REQUIRE(str1.compareTo(str2) == 0); } -TEST_CASE ("Testing & String::operator = (const char *)", "[String-operator+-14]") +TEST_CASE ("Testing & String::operator = (const char *) with NULL does not leave string unchanged", "[String-operator+-14]") { char *buffer = NULL; arduino::String str("Hello"); str = buffer; - REQUIRE(str.compareTo("Hello") == 0); + REQUIRE(str.compareTo("Hello") != 0); +} + +TEST_CASE ("Testing & String::operator = (const char *) with NULL produces invalid string", "[String-operator+-14]") +{ + char *buffer = NULL; + arduino::String str("Hello"); + str = buffer; + REQUIRE_FALSE(str); } TEST_CASE ("Testing & String::operator = (const String &) with invalid buffer of first string", "[String-operator+-15]")