Skip to content

Commit

Permalink
library: don't ignore map keys when checking json maxDepth
Browse files Browse the repository at this point in the history
e5feb0553ad2842db9974979d9808105e6c5ba34
  • Loading branch information
ionagamed committed Jul 2, 2024
1 parent 936cd85 commit e067a6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions library/cpp/json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ namespace NJson {
value.SetType(JSON_UNDEFINED);
}
S.emplace(&value);
if (!IsWithinStackBounds()) {
return false;
}
return true;
}

Expand Down
36 changes: 32 additions & 4 deletions library/cpp/json/ut/json_reader_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TReformatCallbacks: public TJsonCallbacks {
}
};

void GenerateDeepJson(TStringStream& stream, ui64 depth) {
void GenerateDeepJsonArray(TStringStream& stream, ui64 depth) {
stream << "{\"key\":";
for (ui32 i = 0; i < depth - 1; ++i) {
stream << "[";
Expand All @@ -77,6 +77,16 @@ void GenerateDeepJson(TStringStream& stream, ui64 depth) {
stream << "}";
}

void GenerateDeepJsonDict(TStringStream& stream, ui64 depth) {
for (ui64 i = 0; i < depth - 1; ++i) {
stream << "{\"key\":";
}
stream << "{}";
for (ui64 i = 0; i < depth - 1; ++i) {
stream << "}";
}
}

Y_UNIT_TEST_SUITE(TJsonReaderTest) {
Y_UNIT_TEST(JsonReformatTest) {
TString data = "{\"null value\": null, \"intkey\": 10, \"double key\": 11.11, \"string key\": \"string\", \"array\": [1,2,3,\"TString\"], \"bool key\": true}";
Expand Down Expand Up @@ -414,7 +424,7 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) {
constexpr ui32 brackets = static_cast<ui32>(1e5);

TStringStream jsonStream;
GenerateDeepJson(jsonStream, brackets);
GenerateDeepJsonArray(jsonStream, brackets);

TJsonReaderConfig config;
config.UseIterativeParser = true;
Expand All @@ -429,7 +439,25 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) {

{
TStringStream jsonStream;
GenerateDeepJson(jsonStream, depth);
GenerateDeepJsonArray(jsonStream, depth);
TJsonReaderConfig config;
config.MaxDepth = depth;
TJsonValue v;
UNIT_ASSERT(ReadJsonTree(&jsonStream, &config, &v));
}

{
TStringStream jsonStream;
GenerateDeepJsonArray(jsonStream, depth);
TJsonReaderConfig config;
config.MaxDepth = depth - 1;
TJsonValue v;
UNIT_ASSERT(!ReadJsonTree(&jsonStream, &config, &v));
}

{
TStringStream jsonStream;
GenerateDeepJsonDict(jsonStream, depth);
TJsonReaderConfig config;
config.MaxDepth = depth;
TJsonValue v;
Expand All @@ -438,7 +466,7 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) {

{
TStringStream jsonStream;
GenerateDeepJson(jsonStream, depth);
GenerateDeepJsonDict(jsonStream, depth);
TJsonReaderConfig config;
config.MaxDepth = depth - 1;
TJsonValue v;
Expand Down

0 comments on commit e067a6f

Please sign in to comment.