aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2023-01-26 10:55:34 +0100
committerm-holger <m-holger@kubitscheck.org>2023-02-01 15:18:57 +0100
commit83f972ceda20e244f52bde7ac052e6931a6d33d3 (patch)
treef9c855613ccf823755c91086ae6ab4d3b73bf09e
parent8fd6e1c5b24d0fc804c25004f398b39b5041b034 (diff)
downloadqpdf-83f972ceda20e244f52bde7ac052e6931a6d33d3.tar.zst
Refactor end of input handling in JSONParser
-rw-r--r--libqpdf/JSON.cc62
-rw-r--r--libtests/libtests.testcov3
-rw-r--r--libtests/qtest/json_parse/bad-27.out2
-rw-r--r--libtests/qtest/json_parse/bad-28.out2
-rw-r--r--libtests/qtest/json_parse/bad-34.out2
5 files changed, 16 insertions, 55 deletions
diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc
index 3f1a3879..6ee11309 100644
--- a/libqpdf/JSON.cc
+++ b/libqpdf/JSON.cc
@@ -1012,7 +1012,7 @@ JSONParser::getToken()
case ls_number:
// We only get here after we have seen an exponent.
if ((*p >= '0') && (*p <= '9')) {
- ++number_after_e;
+ ++number_after_e;
} else if (QUtil::is_space(*p)) {
action = ignore;
ready = true;
@@ -1093,38 +1093,27 @@ JSONParser::getToken()
}
}
if (done) {
- if ((!token.empty()) && (!ready)) {
+ if (!token.empty() && !ready) {
switch (lex_state) {
case ls_top:
// Can't happen
throw std::logic_error("tok_start set in ls_top while parsing");
break;
- case ls_number:
- case ls_number_minus:
case ls_number_leading_zero:
case ls_number_before_point:
- case ls_number_point:
case ls_number_after_point:
- case ls_number_e:
- case ls_number_e_sign:
- case ls_alpha:
- // okay
+ lex_state = ls_number;
break;
- case ls_u4:
- QTC::TC("libtests", "JSON parse premature end of u");
- throw std::runtime_error(
- "JSON: offset " + std::to_string(offset - u_count - 1) +
- ": \\u must be followed by four characters");
-
- case ls_string:
- case ls_backslash:
- QTC::TC("libtests", "JSON parse unterminated string");
- throw std::runtime_error(
- "JSON: offset " + std::to_string(offset) +
- ": unterminated string");
+ case ls_number:
+ case ls_alpha:
+ // terminal state
break;
+
+ default:
+ QTC::TC("libtests", "JSON parse ls premature end of input");
+ throw std::runtime_error("JSON: premature end of input");
}
}
}
@@ -1181,32 +1170,6 @@ JSONParser::handleToken()
break;
case ls_number:
- case ls_number_minus:
- case ls_number_leading_zero:
- case ls_number_before_point:
- case ls_number_point:
- case ls_number_after_point:
- case ls_number_e:
- case ls_number_e_sign:
- if (number_saw_point && (number_after_point == 0)) {
- // QTC::TC("libtests", "JSON parse decimal with no digits");
- throw std::runtime_error(
- "JSON: offset " + std::to_string(offset) +
- ": decimal point with no digits");
- }
- if ((number_before_point > 1) &&
- ((first_char == '0') ||
- ((first_char == '-') && (token.at(1) == '0')))) {
- throw std::runtime_error(
- "JSON: offset " + std::to_string(offset) +
- ": number with leading zero");
- }
- if ((number_before_point == 0) && (number_after_point == 0)) {
- // QTC::TC("libtests", "JSON parse number no digits");
- throw std::runtime_error(
- "JSON: offset " + std::to_string(offset) +
- ": number with no digits");
- }
item = std::make_shared<JSON>(JSON::makeNumber(token));
break;
@@ -1229,10 +1192,9 @@ JSONParser::handleToken()
item = std::make_shared<JSON>(JSON::makeString(s_value));
break;
- case ls_backslash:
- case ls_u4:
+ default:
throw std::logic_error(
- "tok_end is set while state = ls_backslash or ls_u4");
+ "JSONParser::handleToken : non-terminal lexer state encountered");
break;
}
diff --git a/libtests/libtests.testcov b/libtests/libtests.testcov
index 5ca5fb77..4b3bb45b 100644
--- a/libtests/libtests.testcov
+++ b/libtests/libtests.testcov
@@ -79,9 +79,8 @@ JSON parse number minus no digits 0
JSON parse incomplete number 0
JSON parse keyword bad character 0
JSON parse backslash bad character 0
-JSON parse unterminated string 0
JSON parse leading zero 0
-JSON parse premature end of u 0
+JSON parse ls premature end of input 0
JSON parse bad hex after u 0
JSONHandler unhandled value 0
JSONHandler unexpected key 0
diff --git a/libtests/qtest/json_parse/bad-27.out b/libtests/qtest/json_parse/bad-27.out
index 2c2df076..70fcbf74 100644
--- a/libtests/qtest/json_parse/bad-27.out
+++ b/libtests/qtest/json_parse/bad-27.out
@@ -1 +1 @@
-exception: bad-27.json: JSON: offset 6: unterminated string
+exception: bad-27.json: JSON: premature end of input
diff --git a/libtests/qtest/json_parse/bad-28.out b/libtests/qtest/json_parse/bad-28.out
index d7db2aea..005a68d2 100644
--- a/libtests/qtest/json_parse/bad-28.out
+++ b/libtests/qtest/json_parse/bad-28.out
@@ -1 +1 @@
-exception: bad-28.json: JSON: offset 16: unterminated string
+exception: bad-28.json: JSON: premature end of input
diff --git a/libtests/qtest/json_parse/bad-34.out b/libtests/qtest/json_parse/bad-34.out
index f9db587a..c21838c4 100644
--- a/libtests/qtest/json_parse/bad-34.out
+++ b/libtests/qtest/json_parse/bad-34.out
@@ -1 +1 @@
-exception: bad-34.json: JSON: offset 3: \u must be followed by four characters
+exception: bad-34.json: JSON: premature end of input