diff --git a/src/jv_parse.c b/src/jv_parse.c index 506cd116..0f1df5ad 100644 --- a/src/jv_parse.c +++ b/src/jv_parse.c @@ -63,6 +63,7 @@ struct jv_parser { JV_PARSER_WAITING_FOR_RS // parse error, waiting for RS } st; unsigned int last_ch_was_ws:1; + unsigned int skipping_error:1; // skipping the rest of a reported error }; @@ -90,6 +91,7 @@ static void parser_init(struct jv_parser* p, int flags) { p->curr_buf = 0; p->curr_buf_length = p->curr_buf_pos = p->curr_buf_is_partial = 0; p->last_ch_was_ws = 0; + p->skipping_error = 0; p->line = 1; p->column = 0; jvp_dtoa_context_init(&p->dtoa); @@ -773,8 +775,10 @@ jv jv_parser_next(struct jv_parser* p) { } else { p->column++; } - if (ch == '\036') + if (ch == '\036') { p->st = JV_PARSER_NORMAL; + p->skipping_error = 0; + } continue; // need to resync, wait for RS } msg = scan(p, ch, &value); @@ -789,6 +793,7 @@ jv jv_parser_next(struct jv_parser* p) { // Skip to the next RS. This must come after parser_reset(), which // sets the state back to JV_PARSER_NORMAL. p->st = JV_PARSER_WAITING_FOR_RS; + p->skipping_error = 1; return value; } value = make_error(p, "%s at line %d, column %d", msg, p->line, p->column); @@ -809,8 +814,11 @@ jv jv_parser_next(struct jv_parser* p) { p->eof = 1; assert(p->curr_buf_pos == p->curr_buf_length); jv_free(value); - if (p->st == JV_PARSER_WAITING_FOR_RS) + if (p->st == JV_PARSER_WAITING_FOR_RS) { + if (p->skipping_error) + return jv_invalid(); return make_error(p, "Unfinished abandoned text at EOF at line %d, column %d", p->line, p->column); + } if (p->st != JV_PARSER_NORMAL) { value = make_error(p, "Unfinished string at EOF at line %d, column %d", p->line, p->column); parser_reset(p); diff --git a/tests/shtest b/tests/shtest index 9d1bfcff..89984cfe 100755 --- a/tests/shtest +++ b/tests/shtest @@ -157,6 +157,17 @@ EOF printf '\036{"a":x,"b":7}\n\036[1,}2\n\0363\n' | $VALGRIND $Q $JQ -ces --seq '. == [3]' > /dev/null 2> $d/out cmp $d/out $d/expected +# The skipped rest of a broken last text is not reported again at EOF +cat > $d/expected < /dev/null 2> $d/out +cmp $d/out $d/expected + +printf '\036["Invalid numeric literal at line 1, column 8 (need RS to resync)",["a"]]\n' > $d/expected +printf '\036{"a":x}\n' | $VALGRIND $Q $JQ -c --seq --stream-errors . > $d/out 2>&1 +cmp $d/out $d/expected + # Note that here jq sees no inputs at all but it still succeeds because # --seq ignores parse errors cat > $d/expected <