From a9835d6c37d2a3c762c4620294d0b2a13d543ccb Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 20 Jun 2021 09:07:45 -0700 Subject: read/get-json: reject trailing junk in string input. * parser.c (lisp_parse_impl): If parsing from string, check for trailing junk and diagnose. JSON parsing doesn't use lookahead because it doesn't have a.b syntax, so the recent_tok gives the last token that actually went into the syntax, and not a lookahead token. So in the case of JSON, we call yylex to see if there is any trailing token. * tests/010/json.tl: Extend get-json tests to more kinds of objects, and then replicate with trailing whitespace and trailing junk to provide coverage for these cases. * tests/012/parse.t: Slew of new read tests and iread also. * txr.1: Documented. --- parser.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'parser.c') diff --git a/parser.c b/parser.c index bc01c2bf..fd274ebb 100644 --- a/parser.c +++ b/parser.c @@ -636,11 +636,12 @@ static val lisp_parse_impl(val self, enum prime_parser prime, { uses_or2; val source = default_null_arg(source_in); - val input_stream = if3(stringp(source), + val str = stringp(source); + val input_stream = if3(str, make_string_byte_input_stream(source), or2(source, std_input)); val name = or2(default_null_arg(name_in), - if3(stringp(source), + if3(str, lit("string"), stream_get_prop(input_stream, name_k))); val parser = ensure_parser(input_stream, name); @@ -676,6 +677,19 @@ static val lisp_parse_impl(val self, enum prime_parser prime, break; } + if (str) { + int junk = 0; + if (prime == prime_json) { + yystype yyl; + junk = yylex(&yyl, pi->scanner); + } else { + junk = pi->recent_tok.yy_char; + } + + if (junk) + yyerrorf(pi->scanner, lit("trailing material after expression"), nao); + } + parsed = t; uw_unwind { -- cgit v1.2.3