Do not report the skipped rest of a broken --seq text again at EOF (#3680)

When the last text of a --seq input had a parse error, the parser
reached EOF while skipping to the next RS and reported the same text
again as unfinished abandoned text.

Remember that the skipped text was already reported, so that the input
ends quietly at EOF. Text before the first RS is still reported as
abandoned.
This commit is contained in:
Jay Hemnani authored and GitHub committed 2026-12-05 21:55:29 +09:00
1 parent 0777ba33fc
commit 50a35f7b41
2 files changed
+21 -2

No files matched your search

+10 -2
View File
@@ -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);
+11
View File
@@ -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 <<EOF
jq: ignoring parse error: Invalid numeric literal at line 2, column 8 (need RS to resync)
EOF
printf '\0361\n\036{"a":x}\n' | $VALGRIND $Q $JQ -ces --seq '. == [1]' > /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 <<EOF