Increased default nesting limit to 50 when compiled for a computer (issue #349)

This commit is contained in:
Benoit Blanchon
2016-09-21 22:11:38 +02:00
parent bb805e93cb
commit e6f55b1f6f
4 changed files with 33 additions and 8 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Increased default nesting limit to 50 when compiled for a computer (issue #349)
v5.6.7 v5.6.7
------ ------

View File

@ -42,6 +42,11 @@
#endif #endif
#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 #else // assume this is a computer
// on a computer we have plenty of memory so we can use doubles // on a computer we have plenty of memory so we can use doubles
@ -82,6 +87,11 @@
#define ARDUINOJSON_ENABLE_ALIGNMENT 1 #define ARDUINOJSON_ENABLE_ALIGNMENT 1
#endif #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 #endif
#if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64

View File

@ -117,7 +117,9 @@ class JsonBuffer {
char *strdup(const char *src) { char *strdup(const char *src) {
return src ? strdup(src, strlen(src)) : NULL; 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. // Allocates n bytes in the JsonBuffer.
// Return a pointer to the allocated memory or NULL if allocation fails. // 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(). // Default value of nesting limit of parseArray() and parseObject().
// //
// The nesting limit is a contain on the level of nesting allowed in the // The nesting limit is a constrain on the level of nesting allowed in the
// JSON // JSON string.
// string.
// If set to 0, only a flat array or objects can be parsed. // 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 // If set to 1, the object can contain nested arrays or objects but only 1
// level deep. // level deep.
@ -150,7 +151,7 @@ class JsonBuffer {
// The purpose of this feature is to prevent stack overflow that could // The purpose of this feature is to prevent stack overflow that could
// lead to // lead to
// a security risk. // a security risk.
static const uint8_t DEFAULT_LIMIT = 10; static const uint8_t DEFAULT_LIMIT = ARDUINOJSON_DEFAULT_NESTING_LIMIT;
}; };
} }

View File

@ -5,12 +5,14 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonParser_Array_Tests : public testing::Test { class JsonParser_Array_Tests : public testing::Test {
protected: 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) { void whenInputIs(const char *json, size_t len) {
memcpy(_jsonString, json, len); memcpy(_jsonString, json, len);
@ -27,7 +29,9 @@ class JsonParser_Array_Tests : public testing::Test {
EXPECT_EQ(0, _array->size()); EXPECT_EQ(0, _array->size());
} }
void sizeMustBe(int expected) { ASSERT_EQ(expected, _array->size()); } void sizeMustBe(int expected) {
ASSERT_EQ(expected, _array->size());
}
template <typename T> template <typename T>
void firstElementMustBe(T expected) { void firstElementMustBe(T expected) {
@ -346,3 +350,8 @@ TEST_F(JsonParser_Array_Tests, UnfinishedCComment) {
whenInputIs("[/*COMMENT]"); whenInputIs("[/*COMMENT]");
parseMustFail(); parseMustFail();
} }
TEST_F(JsonParser_Array_Tests, DeeplyNested) {
whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
parseMustSucceed();
}