Skip to content

Commit

Permalink
fix(formats/json): cleanup stack before throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
moki committed Dec 12, 2023
1 parent 8be8327 commit 5edab34
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions universal/src/formats/json/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void CheckKeyUniqueness(const impl::Value* root) {
stack.emplace_back(); // fake "top" frame to avoid extra checks for an empty
// stack inside walker loop
KeysStack keys;
size_t depth = 1;
size_t depth = 0;
for (;;) {
stack.back().Advance();
if (value->IsObject()) {
Expand All @@ -70,15 +70,19 @@ void CheckKeyUniqueness(const impl::Value* root) {

if ((value->IsObject() && value->MemberCount() > 0) ||
(value->IsArray() && value->Size() > 0)) {
depth++;
if (depth > kJsonDepthLimit) {
throw ParseException("Exceeded maximum allowed JSON depth of: " + std::to_string(kJsonDepthLimit));
if (stack.size() >= kJsonDepthLimit) {
while (!stack.back().HasMoreElements()) {
stack.pop_back();
if (stack.empty()) {
throw ParseException("Exceeded maximum allowed JSON depth of: " +
std::to_string(kJsonDepthLimit));
}
}
}
// descend
stack.emplace_back(value);
} else {
while (!stack.back().HasMoreElements()) {
depth--;
stack.pop_back();
if (stack.empty()) return;
}
Expand Down

0 comments on commit 5edab34

Please sign in to comment.