From bff77abe6a79c50e3dde37e39e2a6f171bc6e047 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 3 Jun 2017 16:22:26 +0200 Subject: [PATCH] Fixed reading "true" as a float (issue #516) --- CHANGELOG.md | 1 + src/ArduinoJson/JsonVariantImpl.hpp | 2 -- src/ArduinoJson/Polyfills/parseFloat.hpp | 3 ++- src/ArduinoJson/Polyfills/parseInteger.hpp | 4 +++- test/Polyfills/parseFloat.cpp | 10 ++++++++++ test/Polyfills/parseInteger.cpp | 14 ++++++++++---- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42e99b73..69aa7cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ HEAD * Fixed IntelliSense errors in Visual Micro (issue #483) * Fixed compilation in IAR Embedded Workbench (issue #515) +* Fixed reading "true" as a float (issue #516) * Added `ARDUINOJSON_DOUBLE_IS_64BITS` * Added `ARDUINOJSON_EMBEDDED_MODE` diff --git a/src/ArduinoJson/JsonVariantImpl.hpp b/src/ArduinoJson/JsonVariantImpl.hpp index 1700f1b7..eb6a4735 100644 --- a/src/ArduinoJson/JsonVariantImpl.hpp +++ b/src/ArduinoJson/JsonVariantImpl.hpp @@ -61,8 +61,6 @@ inline T JsonVariant::variantAsInteger() const { return T(~_content.asInteger + 1); case JSON_STRING: case JSON_UNPARSED: - if (!_content.asString) return 0; - if (!strcmp("true", _content.asString)) return 1; return Polyfills::parseInteger(_content.asString); default: return T(_content.asFloat); diff --git a/src/ArduinoJson/Polyfills/parseFloat.hpp b/src/ArduinoJson/Polyfills/parseFloat.hpp index 3d0caa54..d3ed2397 100644 --- a/src/ArduinoJson/Polyfills/parseFloat.hpp +++ b/src/ArduinoJson/Polyfills/parseFloat.hpp @@ -20,7 +20,7 @@ inline T parseFloat(const char* s) { typedef typename traits::mantissa_type mantissa_t; typedef typename traits::exponent_type exponent_t; - if (!s) return 0; + if (!s) return 0; // NULL bool negative_result = false; switch (*s) { @@ -30,6 +30,7 @@ inline T parseFloat(const char* s) { s++; } + if (*s == 't') return 1; // true if (*s == 'n' || *s == 'N') return traits::nan(); if (*s == 'i' || *s == 'I') return negative_result ? -traits::inf() : traits::inf(); diff --git a/src/ArduinoJson/Polyfills/parseInteger.hpp b/src/ArduinoJson/Polyfills/parseInteger.hpp index 2845cd86..7d353fb4 100644 --- a/src/ArduinoJson/Polyfills/parseInteger.hpp +++ b/src/ArduinoJson/Polyfills/parseInteger.hpp @@ -16,7 +16,9 @@ namespace ArduinoJson { namespace Polyfills { template T parseInteger(const char *s) { - if (!s) return 0; + if (!s) return 0; // NULL + + if (*s == 't') return 1; // "true" T result = 0; bool negative_result = false; diff --git a/test/Polyfills/parseFloat.cpp b/test/Polyfills/parseFloat.cpp index 11afaa2a..c0e49b4f 100644 --- a/test/Polyfills/parseFloat.cpp +++ b/test/Polyfills/parseFloat.cpp @@ -101,6 +101,11 @@ TEST_CASE("parseFloat()") { checkInf("+inf", false); checkInf("-inf", true); } + + SECTION("Boolean") { + check("false", 0.0f); + check("true", 1.0f); + } } TEST_CASE("parseFloat()") { @@ -167,4 +172,9 @@ TEST_CASE("parseFloat()") { checkNaN("NaN"); checkNaN("nan"); } + + SECTION("Boolean") { + check("false", 0.0); + check("true", 1.0); + } } diff --git a/test/Polyfills/parseInteger.cpp b/test/Polyfills/parseInteger.cpp index 0392dbf0..fbd23f3d 100644 --- a/test/Polyfills/parseInteger.cpp +++ b/test/Polyfills/parseInteger.cpp @@ -23,11 +23,12 @@ TEST_CASE("parseInteger()") { check("127", 127); check("+127", 127); check("3.14", 3); - // check(" 42", 0); check("x42", 0); check("128", -128); check("-129", 127); check(NULL, 0); + check("true", 1); + check("false", 0); } TEST_CASE("parseInteger()") { @@ -35,11 +36,12 @@ TEST_CASE("parseInteger()") { check("32767", 32767); check("+32767", 32767); check("3.14", 3); - // check(" 42", 0); check("x42", 0); check("-32769", 32767); check("32768", -32768); check(NULL, 0); + check("true", 1); + check("false", 0); } TEST_CASE("parseInteger()") { @@ -47,10 +49,11 @@ TEST_CASE("parseInteger()") { check("2147483647", 2147483647); check("+2147483647", 2147483647); check("3.14", 3); - // check(" 42", 0); check("x42", 0); check("-2147483649", 2147483647); check("2147483648", (-2147483647 - 1)); + check("true", 1); + check("false", 0); } TEST_CASE("parseInteger()") { @@ -58,10 +61,11 @@ TEST_CASE("parseInteger()") { check("255", 255); check("+255", 255); check("3.14", 3); - // check(" 42", 0); check("x42", 0); check("-1", 255); check("256", 0); + check("true", 1); + check("false", 0); } TEST_CASE("parseInteger()") { @@ -73,4 +77,6 @@ TEST_CASE("parseInteger()") { check("x42", 0); check("-1", 65535); check("65536", 0); + check("true", 1); + check("false", 0); }