Commit f2e46c20b62aa72a984b99c816176cfa3367a6e7
1 parent
0de032bc
In JSONParser::handleToken move remaining validations into second switch statement
Showing
1 changed file
with
21 additions
and
50 deletions
libqpdf/JSON.cc
| ... | ... | @@ -1252,56 +1252,6 @@ JSONParser::handleToken() |
| 1252 | 1252 | break; |
| 1253 | 1253 | } |
| 1254 | 1254 | |
| 1255 | - // See whether what we have is allowed at this point. | |
| 1256 | - | |
| 1257 | - if (item.get()) { | |
| 1258 | - switch (parser_state) { | |
| 1259 | - case ps_done: | |
| 1260 | - throw std::logic_error("can't happen; ps_done already handled"); | |
| 1261 | - break; | |
| 1262 | - | |
| 1263 | - case ps_dict_after_key: | |
| 1264 | - QTC::TC("libtests", "JSON parse expected colon"); | |
| 1265 | - throw std::runtime_error( | |
| 1266 | - "JSON: offset " + std::to_string(offset) + ": expected ':'"); | |
| 1267 | - break; | |
| 1268 | - | |
| 1269 | - case ps_dict_after_item: | |
| 1270 | - QTC::TC("libtests", "JSON parse expected , or }"); | |
| 1271 | - throw std::runtime_error( | |
| 1272 | - "JSON: offset " + std::to_string(offset) + | |
| 1273 | - ": expected ',' or '}'"); | |
| 1274 | - break; | |
| 1275 | - | |
| 1276 | - case ps_array_after_item: | |
| 1277 | - QTC::TC("libtests", "JSON parse expected, or ]"); | |
| 1278 | - throw std::runtime_error( | |
| 1279 | - "JSON: offset " + std::to_string(offset) + | |
| 1280 | - ": expected ',' or ']'"); | |
| 1281 | - break; | |
| 1282 | - | |
| 1283 | - case ps_dict_begin: | |
| 1284 | - case ps_dict_after_comma: | |
| 1285 | - if (lex_state != ls_string) { | |
| 1286 | - QTC::TC("libtests", "JSON parse string as dict key"); | |
| 1287 | - throw std::runtime_error( | |
| 1288 | - "JSON: offset " + std::to_string(offset) + | |
| 1289 | - ": expect string as dictionary key"); | |
| 1290 | - } | |
| 1291 | - break; | |
| 1292 | - | |
| 1293 | - case ps_top: | |
| 1294 | - case ps_dict_after_colon: | |
| 1295 | - case ps_array_begin: | |
| 1296 | - case ps_array_after_comma: | |
| 1297 | - break; | |
| 1298 | - // okay | |
| 1299 | - } | |
| 1300 | - } | |
| 1301 | - | |
| 1302 | - // Now we know we have a delimiter or item that is allowed. Do | |
| 1303 | - // whatever we need to do with it. | |
| 1304 | - | |
| 1305 | 1255 | parser_state_e next_state = ps_top; |
| 1306 | 1256 | |
| 1307 | 1257 | item->setStart(token_start); |
| ... | ... | @@ -1310,6 +1260,12 @@ JSONParser::handleToken() |
| 1310 | 1260 | switch (parser_state) { |
| 1311 | 1261 | case ps_dict_begin: |
| 1312 | 1262 | case ps_dict_after_comma: |
| 1263 | + if (lex_state != ls_string) { | |
| 1264 | + QTC::TC("libtests", "JSON parse string as dict key"); | |
| 1265 | + throw std::runtime_error( | |
| 1266 | + "JSON: offset " + std::to_string(offset) + | |
| 1267 | + ": expect string as dictionary key"); | |
| 1268 | + } | |
| 1313 | 1269 | this->dict_key = s_value; |
| 1314 | 1270 | this->dict_key_offset = item->getStart(); |
| 1315 | 1271 | item = nullptr; |
| ... | ... | @@ -1342,8 +1298,23 @@ JSONParser::handleToken() |
| 1342 | 1298 | break; |
| 1343 | 1299 | |
| 1344 | 1300 | case ps_dict_after_key: |
| 1301 | + QTC::TC("libtests", "JSON parse expected colon"); | |
| 1302 | + throw std::runtime_error( | |
| 1303 | + "JSON: offset " + std::to_string(offset) + ": expected ':'"); | |
| 1304 | + break; | |
| 1305 | + | |
| 1345 | 1306 | case ps_dict_after_item: |
| 1307 | + QTC::TC("libtests", "JSON parse expected , or }"); | |
| 1308 | + throw std::runtime_error( | |
| 1309 | + "JSON: offset " + std::to_string(offset) + ": expected ',' or '}'"); | |
| 1310 | + break; | |
| 1311 | + | |
| 1346 | 1312 | case ps_array_after_item: |
| 1313 | + QTC::TC("libtests", "JSON parse expected, or ]"); | |
| 1314 | + throw std::runtime_error( | |
| 1315 | + "JSON: offset " + std::to_string(offset) + ": expected ',' or ']'"); | |
| 1316 | + break; | |
| 1317 | + | |
| 1347 | 1318 | case ps_done: |
| 1348 | 1319 | throw std::logic_error( |
| 1349 | 1320 | "JSONParser::handleToken: unexpected parser state"); | ... | ... |