diff --git a/CHANGELOG.md b/CHANGELOG.md index 070cdcf5..8efccbc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Increased default nesting limit to 50 when compiled for a computer (issue #349) + v5.6.7 ------ diff --git a/include/ArduinoJson/Configuration.hpp b/include/ArduinoJson/Configuration.hpp index c0b88ece..073bd652 100644 --- a/include/ArduinoJson/Configuration.hpp +++ b/include/ArduinoJson/Configuration.hpp @@ -42,6 +42,11 @@ #endif #endif +// low value to prevent stack overflow +#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT +#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 10 +#endif + #else // assume this is a computer // on a computer we have plenty of memory so we can use doubles @@ -82,6 +87,11 @@ #define ARDUINOJSON_ENABLE_ALIGNMENT 1 #endif +// on a computer, we should have a lot of space on the stack +#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT +#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 50 +#endif + #endif #if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64 diff --git a/include/ArduinoJson/JsonBuffer.hpp b/include/ArduinoJson/JsonBuffer.hpp index aae5f21e..f05449dd 100644 --- a/include/ArduinoJson/JsonBuffer.hpp +++ b/include/ArduinoJson/JsonBuffer.hpp @@ -117,7 +117,9 @@ class JsonBuffer { char *strdup(const char *src) { return src ? strdup(src, strlen(src)) : NULL; } - char *strdup(const String &src) { return strdup(src.c_str(), src.length()); } + char *strdup(const String &src) { + return strdup(src.c_str(), src.length()); + } // Allocates n bytes in the JsonBuffer. // Return a pointer to the allocated memory or NULL if allocation fails. @@ -139,9 +141,8 @@ class JsonBuffer { // Default value of nesting limit of parseArray() and parseObject(). // - // The nesting limit is a contain on the level of nesting allowed in the - // JSON - // string. + // The nesting limit is a constrain on the level of nesting allowed in the + // JSON string. // If set to 0, only a flat array or objects can be parsed. // If set to 1, the object can contain nested arrays or objects but only 1 // level deep. @@ -150,7 +151,7 @@ class JsonBuffer { // The purpose of this feature is to prevent stack overflow that could // lead to // a security risk. - static const uint8_t DEFAULT_LIMIT = 10; + static const uint8_t DEFAULT_LIMIT = ARDUINOJSON_DEFAULT_NESTING_LIMIT; }; } diff --git a/test/JsonParser_Array_Tests.cpp b/test/JsonParser_Array_Tests.cpp index 4b69e1a5..05da0975 100644 --- a/test/JsonParser_Array_Tests.cpp +++ b/test/JsonParser_Array_Tests.cpp @@ -5,12 +5,14 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonParser_Array_Tests : public testing::Test { protected: - void whenInputIs(const char *json) { strcpy(_jsonString, json); } + void whenInputIs(const char *json) { + strcpy(_jsonString, json); + } void whenInputIs(const char *json, size_t len) { memcpy(_jsonString, json, len); @@ -27,7 +29,9 @@ class JsonParser_Array_Tests : public testing::Test { EXPECT_EQ(0, _array->size()); } - void sizeMustBe(int expected) { ASSERT_EQ(expected, _array->size()); } + void sizeMustBe(int expected) { + ASSERT_EQ(expected, _array->size()); + } template void firstElementMustBe(T expected) { @@ -346,3 +350,8 @@ TEST_F(JsonParser_Array_Tests, UnfinishedCComment) { whenInputIs("[/*COMMENT]"); parseMustFail(); } + +TEST_F(JsonParser_Array_Tests, DeeplyNested) { + whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]"); + parseMustSucceed(); +}