Commit 29093a167b3f628d23b5a7890404eab659c6a685

Authored by m-holger
1 parent a39043f6

In JSONParser::handleToken refactor container creation

Showing 1 changed file with 17 additions and 22 deletions
libqpdf/JSON.cc
... ... @@ -1294,6 +1294,7 @@ JSONParser::handleToken()
1294 1294 break;
1295 1295  
1296 1296 case ps_top:
  1297 + stack.push_back(item);
1297 1298 next_state = ps_done;
1298 1299 break;
1299 1300  
... ... @@ -1320,36 +1321,30 @@ JSONParser::handleToken()
1320 1321 "JSONParser::handleToken: unexpected parser state");
1321 1322 }
1322 1323  
1323   - if (reactor) {
  1324 + if (item->isDictionary() || item->isArray()) {
  1325 + stack.push_back(item);
  1326 + ps_stack.push_back(next_state);
1324 1327 // Calling container start method is postponed until after
1325 1328 // adding the containers to their parent containers, if any.
1326 1329 // This makes it much easier to keep track of the current
1327 1330 // nesting level.
1328 1331 if (item->isDictionary()) {
1329   - reactor->dictionaryStart();
  1332 + if (reactor) {
  1333 + reactor->dictionaryStart();
  1334 + }
  1335 + next_state = ps_dict_begin;
1330 1336 } else if (item->isArray()) {
1331   - reactor->arrayStart();
  1337 + if (reactor) {
  1338 + reactor->arrayStart();
  1339 + }
  1340 + next_state = ps_array_begin;
1332 1341 }
1333   - }
1334   -
1335   - // Prepare for next token
1336 1342  
1337   - if (item->isDictionary()) {
1338   - stack.push_back(item);
1339   - ps_stack.push_back(next_state);
1340   - next_state = ps_dict_begin;
1341   - } else if (item->isArray()) {
1342   - stack.push_back(item);
1343   - ps_stack.push_back(next_state);
1344   - next_state = ps_array_begin;
1345   - } else if (parser_state == ps_top) {
1346   - stack.push_back(item);
1347   - }
1348   -
1349   - if (ps_stack.size() > 500) {
1350   - throw std::runtime_error(
1351   - "JSON: offset " + std::to_string(offset) +
1352   - ": maximum object depth exceeded");
  1343 + if (ps_stack.size() > 500) {
  1344 + throw std::runtime_error(
  1345 + "JSON: offset " + std::to_string(offset) +
  1346 + ": maximum object depth exceeded");
  1347 + }
1353 1348 }
1354 1349 parser_state = next_state;
1355 1350 }
... ...