From f831ed395d275dc56418df9f2c9403c4f56e16d1 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 17 Feb 2022 11:16:05 +0100 Subject: [PATCH] Fix `JsonString` operator `==` and `!=` for non-zero-terminated string --- CHANGELOG.md | 1 + extras/tests/Misc/JsonString.cpp | 39 +++++++++++++++++++----------- src/ArduinoJson/Strings/String.hpp | 12 +++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d67a779e..4b7a9ef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Fix `call of overloaded 'String(const char*, int)' is ambiguous` +* Fix `JsonString` operator `==` and `!=` for non-zero-terminated string v6.19.2 (2022-02-14) ------- diff --git a/extras/tests/Misc/JsonString.cpp b/extras/tests/Misc/JsonString.cpp index b7d606fd..557f9936 100644 --- a/extras/tests/Misc/JsonString.cpp +++ b/extras/tests/Misc/JsonString.cpp @@ -14,44 +14,53 @@ TEST_CASE("JsonString") { CHECK(s.isNull() == true); CHECK(s.c_str() == 0); CHECK(s.isLinked() == true); + CHECK(s == JsonString()); + CHECK(s != ""); } - SECTION("Compare null with boolean") { + SECTION("Null converts to false") { JsonString s; CHECK(bool(s) == false); - CHECK(false == bool(s)); - CHECK(bool(s) != true); - CHECK(true != bool(s)); } - SECTION("Compare non-null with boolean") { - JsonString s("hello"); + SECTION("Empty string converts to true") { + JsonString s(""); + CHECK(bool(s) == true); - CHECK(true == bool(s)); - CHECK(bool(s) != false); - CHECK(false != bool(s)); } - SECTION("Compare null with null") { + SECTION("Non-empty string converts to true") { + JsonString s(""); + + CHECK(bool(s) == true); + } + + SECTION("Null strings equals each others") { JsonString a, b; CHECK(a == b); CHECK_FALSE(a != b); } - SECTION("Compare null with non-null") { - JsonString a(0), b("hello"); + SECTION("Null and empty strings differ") { + JsonString a, b(""); CHECK_FALSE(a == b); CHECK(a != b); + + CHECK_FALSE(b == a); + CHECK(b != a); } - SECTION("Compare non-null with null") { - JsonString a("hello"), b(0); + SECTION("Null and non-empty strings differ") { + JsonString a, b("hello"); CHECK_FALSE(a == b); CHECK(a != b); + + CHECK_FALSE(b == a); + CHECK(b != a); } SECTION("Compare different strings") { @@ -88,5 +97,7 @@ TEST_CASE("JsonString") { CHECK(s.size() == 5); CHECK(s.isLinked() == true); + CHECK(s == "hello"); + CHECK(s != "hello world"); } } diff --git a/src/ArduinoJson/Strings/String.hpp b/src/ArduinoJson/Strings/String.hpp index 08956f05..1371114e 100644 --- a/src/ArduinoJson/Strings/String.hpp +++ b/src/ArduinoJson/Strings/String.hpp @@ -46,23 +46,19 @@ class String : public SafeBoolIdom { } friend bool operator==(String lhs, String rhs) { + if (lhs._size != rhs._size) + return false; if (lhs._data == rhs._data) return true; if (!lhs._data) return false; if (!rhs._data) return false; - return strcmp(lhs._data, rhs._data) == 0; + return memcmp(lhs._data, rhs._data, lhs._size) == 0; } friend bool operator!=(String lhs, String rhs) { - if (lhs._data == rhs._data) - return false; - if (!lhs._data) - return true; - if (!rhs._data) - return true; - return strcmp(lhs._data, rhs._data) != 0; + return !(lhs == rhs); } #if ARDUINOJSON_ENABLE_STD_STREAM