From 4dba3c95dd9cc721957f8138fe19ab2872328f27 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 26 Jan 2023 14:10:48 +0000 Subject: In JSONParser::handleToken move validation for ls_colon etc into switch statement --- libqpdf/JSON.cc | 71 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index ef652a86..32fe5730 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -1150,10 +1150,44 @@ JSONParser::handleToken() break; case ls_colon: + if (parser_state != ps_dict_after_key) { + QTC::TC("libtests", "JSON parse unexpected :"); + throw std::runtime_error( + "JSON: offset " + std::to_string(offset) + + ": unexpected colon"); + } + break; + case ls_comma: + if (!((parser_state == ps_dict_after_item) || + (parser_state == ps_array_after_item))) { + QTC::TC("libtests", "JSON parse unexpected ,"); + throw std::runtime_error( + "JSON: offset " + std::to_string(offset) + + ": unexpected comma"); + } + break; + case ls_end_array: + if (!((parser_state == ps_array_begin) || + (parser_state == ps_array_after_item))) + + { + QTC::TC("libtests", "JSON parse unexpected ]"); + throw std::runtime_error( + "JSON: offset " + std::to_string(offset) + + ": unexpected array end delimiter"); + } + break; + case ls_end_dict: - // continue + if (!((parser_state == ps_dict_begin) || + (parser_state == ps_dict_after_item))) { + QTC::TC("libtests", "JSON parse unexpected }"); + throw std::runtime_error( + "JSON: offset " + std::to_string(offset) + + ": unexpected dictionary end delimiter"); + } break; case ls_number: @@ -1235,41 +1269,6 @@ JSONParser::handleToken() break; // okay } - } else if (lex_state == ls_end_dict) { - if (!((parser_state == ps_dict_begin) || - (parser_state == ps_dict_after_item))) - - { - QTC::TC("libtests", "JSON parse unexpected }"); - throw std::runtime_error( - "JSON: offset " + std::to_string(offset) + - ": unexpected dictionary end delimiter"); - } - } else if (lex_state == ls_end_array) { - if (!((parser_state == ps_array_begin) || - (parser_state == ps_array_after_item))) - - { - QTC::TC("libtests", "JSON parse unexpected ]"); - throw std::runtime_error( - "JSON: offset " + std::to_string(offset) + - ": unexpected array end delimiter"); - } - } else if (lex_state == ls_colon) { - if (parser_state != ps_dict_after_key) { - QTC::TC("libtests", "JSON parse unexpected :"); - throw std::runtime_error( - "JSON: offset " + std::to_string(offset) + - ": unexpected colon"); - } - } else if (lex_state == ls_comma) { - if (!((parser_state == ps_dict_after_item) || - (parser_state == ps_array_after_item))) { - QTC::TC("libtests", "JSON parse unexpected ,"); - throw std::runtime_error( - "JSON: offset " + std::to_string(offset) + - ": unexpected comma"); - } } // Now we know we have a delimiter or item that is allowed. Do -- cgit v1.2.3-54-g00ecf