From c5b3a9e08ee1704a3db9e2ecb43b3d2e8ca1e5f1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 24 Feb 2020 11:54:35 +1100 Subject: [PATCH] Have std/json look at args.src.is_closed() more --- example/jsonptr/jsonptr.cc | 5 +++++ release/c/wuffs-unsupported-snapshot.c | 18 ++++++++++++++++++ std/json/decode_json.wuffs | 12 ++++++++++++ 3 files changed, 35 insertions(+) diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index 07a280e2..3e667d75 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -17,6 +17,11 @@ /* jsonptr is a JSON formatter (pretty-printer). +As of 2020-02-24, this program passes all 318 "test_parsing" cases from the +JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the +"Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that +was first published on 2016-10-26 and updated on 2018-03-30. + This example program differs from most other example Wuffs programs in that it is written in C++, not C. diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 15ae7d6e..505b86eb 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -18756,6 +18756,10 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self, label_2_continue:; while (true) { if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) { + if (a_src && a_src->meta.closed) { + status = wuffs_base__make_status(wuffs_json__error__bad_input); + goto exit; + } status = wuffs_base__make_status(wuffs_base__suspension__short_read); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); @@ -18842,6 +18846,11 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self, } } if (((uint64_t)(io2_a_src - iop_a_src)) < 2) { + if (a_src && a_src->meta.closed) { + status = + wuffs_base__make_status(wuffs_json__error__bad_input); + goto exit; + } status = wuffs_base__make_status( wuffs_base__suspension__short_read); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7); @@ -18860,6 +18869,11 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self, goto label_3_continue; } else if (v_backslash == 117) { if (((uint64_t)(io2_a_src - iop_a_src)) < 6) { + if (a_src && a_src->meta.closed) { + status = + wuffs_base__make_status(wuffs_json__error__bad_input); + goto exit; + } status = wuffs_base__make_status( wuffs_base__suspension__short_read); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8); @@ -18913,6 +18927,10 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self, label_4_continue:; while (true) { if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) { + if (a_src && a_src->meta.closed) { + status = wuffs_base__make_status(wuffs_json__error__bad_input); + goto exit; + } status = wuffs_base__make_status(wuffs_base__suspension__short_read); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9); diff --git a/std/json/decode_json.wuffs b/std/json/decode_json.wuffs index 40183db8..7cb6a770 100644 --- a/std/json/decode_json.wuffs +++ b/std/json/decode_json.wuffs @@ -157,6 +157,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { // before issuing a token with value_base_category 4. while true { if args.src.available() <= 0 { + if args.src.is_closed() { + return "#bad input" + } yield? base."$short read" continue } @@ -230,6 +233,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { assert args.dst.available() > 0 if args.src.available() < 2 { + if args.src.is_closed() { + return "#bad input" + } yield? base."$short read" string_length = 0 continue.string_loop @@ -245,6 +251,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { } else if backslash == 0x75 { // 0x75 is 'u'. if args.src.available() < 6 { + if args.src.is_closed() { + return "#bad input" + } yield? base."$short read" string_length = 0 continue.string_loop @@ -291,6 +300,9 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) { // The trailing '"' is filler. while true { if args.src.available() <= 0 { + if args.src.is_closed() { + return "#bad input" + } yield? base."$short read" continue }