Skip to content

Commit

Permalink
Add json nest unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelportilla authored and HowardHinnant committed Jan 16, 2018
1 parent 8c7643a commit de4cc74
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/ripple/json/impl/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace Json
// Implementation of class Reader
// ////////////////////////////////

constexpr unsigned nest_limit = 1000;

static
std::string
codePointToUTF8 (unsigned int cp)
Expand Down Expand Up @@ -143,7 +141,7 @@ Reader::readValue(unsigned depth)
{
Token token;
skipCommentTokens ( token );
if (depth >= nest_limit)
if (depth > nest_limit)
return addError("Syntax error: maximum nesting depth exceeded", token);
bool successful = true;

Expand Down
2 changes: 2 additions & 0 deletions src/ripple/json/json_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Reader
*/
std::string getFormatedErrorMessages () const;

static constexpr unsigned nest_limit {1000};

private:
enum TokenType
{
Expand Down
49 changes: 49 additions & 0 deletions src/test/json/json_value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <ripple/beast/unit_test.h>
#include <ripple/beast/type_name.h>

#include <algorithm>

namespace ripple {

struct json_value_test : beast::unit_test::suite
Expand Down Expand Up @@ -249,6 +251,52 @@ struct json_value_test : beast::unit_test::suite
}
}

void test_nest_limits ()
{
Json::Reader r;
{
auto nest = [](std::uint32_t depth)->std::string {
std::string s = "{";
for (std::uint32_t i{1}; i <= depth; ++i)
s += "\"obj\":{";
for (std::uint32_t i{1}; i <= depth; ++i)
s += "}";
s += "}";
return s;
};

{
// Within object nest limit
auto json{nest(std::min(10u, Json::Reader::nest_limit))};
Json::Value j;
BEAST_EXPECT(r.parse(json, j));
}

{
// Exceed object nest limit
auto json{nest(Json::Reader::nest_limit + 1)};
Json::Value j;
BEAST_EXPECT(!r.parse(json, j));
}
}

auto nest = [](std::uint32_t depth)->std::string {
std::string s = "{";
for (std::uint32_t i{1}; i <= depth; ++i)
s += "\"array\":[{";
for (std::uint32_t i{1}; i <= depth; ++i)
s += "]}";
s += "}";
return s;
};
{
// Exceed array nest limit
auto json{nest(Json::Reader::nest_limit + 1)};
Json::Value j;
BEAST_EXPECT(!r.parse(json, j));
}
}

void run ()
{
test_bool ();
Expand All @@ -258,6 +306,7 @@ struct json_value_test : beast::unit_test::suite
test_move ();
test_comparisons ();
test_compact ();
test_nest_limits ();
}
};

Expand Down

0 comments on commit de4cc74

Please sign in to comment.