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 c5edf01
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions universal/src/formats/json/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ 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;
for (;;) {
stack.back().Advance();
if (value->IsObject()) {
Expand All @@ -70,15 +69,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 c5edf01

Please sign in to comment.