Fix behavior of --slurp --stream when input has no trailing newline character (#3279)

This commit fixes a strange behavior of the combination of `--slurp` and
`--stream` flags when the input has no trailing newline character.
This commit is contained in:
itchyny authored and GitHub committed 2025-05-22 08:04:05 +09:00
1 parent 7d8c096e48
commit 4e3088f188
2 files changed
+11 -12

No files matched your search

+4 -12
View File
@@ -271,12 +271,11 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
fclose(state->current_input); fclose(state->current_input);
} }
state->current_input = NULL; state->current_input = NULL;
jv_free(state->current_filename);
state->current_filename = jv_invalid();
state->current_line = 0 ;
} }
const char *f = next_file(state); const char *f = next_file(state);
if (f != NULL) { if (f != NULL) {
jv_free(state->current_filename);
state->current_line = 0;
if (!strcmp(f, "-")) { if (!strcmp(f, "-")) {
state->current_input = stdin; state->current_input = stdin;
state->current_filename = jv_string("<stdin>"); state->current_filename = jv_string("<stdin>");
@@ -288,7 +287,6 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
state->failures++; state->failures++;
} }
} }
state->current_line = 0;
} }
} }
@@ -344,8 +342,7 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
} }
} }
} }
return state->curr_file == state->nfiles && return state->curr_file == state->nfiles && !state->current_input;
(!state->current_input || feof(state->current_input) || ferror(state->current_input));
} }
jv jq_util_input_next_input_cb(jq_state *jq, void *data) { jv jq_util_input_next_input_cb(jq_state *jq, void *data) {
@@ -399,7 +396,6 @@ jv jq_util_input_get_current_line(jq_state* jq) {
// When slurping, it returns just one value // When slurping, it returns just one value
jv jq_util_input_next_input(jq_util_input_state *state) { jv jq_util_input_next_input(jq_util_input_state *state) {
int is_last = 0; int is_last = 0;
int has_more = 0;
jv value = jv_invalid(); // need more input jv value = jv_invalid(); // need more input
do { do {
if (state->parser == NULL) { if (state->parser == NULL) {
@@ -429,10 +425,6 @@ jv jq_util_input_next_input(jq_util_input_state *state) {
} }
value = jv_parser_next(state->parser); value = jv_parser_next(state->parser);
if (jv_is_valid(state->slurped)) { if (jv_is_valid(state->slurped)) {
// When slurping an input that doesn't have a trailing newline,
// we might have more than one value on the same line, so let's check
// to see if we have more data to parse.
has_more = jv_parser_remaining(state->parser);
if (jv_is_valid(value)) { if (jv_is_valid(value)) {
state->slurped = jv_array_append(state->slurped, value); state->slurped = jv_array_append(state->slurped, value);
value = jv_invalid(); value = jv_invalid();
@@ -442,7 +434,7 @@ jv jq_util_input_next_input(jq_util_input_state *state) {
return value; return value;
} }
} }
} while (!is_last || has_more); } while (!is_last);
if (jv_is_valid(state->slurped)) { if (jv_is_valid(state->slurped)) {
value = state->slurped; value = state->slurped;
+7
View File
@@ -155,6 +155,13 @@ cmp $d/out $d/expected
printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1 printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1
cmp $d/out $d/expected cmp $d/out $d/expected
# Regression test for #3273
echo "[[[0],1],[[0]],[[0],2],[[0]]]" > $d/expected
printf "[1][2]" | $JQ -c -s --stream . > $d/out 2>&1
cmp $d/out $d/expected
printf "[1][2]\n" | $JQ -c -s --stream . > $d/out 2>&1
cmp $d/out $d/expected
# Regression test for --raw-output0 # Regression test for --raw-output0
printf "a\0b\0" > $d/expected printf "a\0b\0" > $d/expected
printf '["a", "b"]' | $VALGRIND $Q $JQ --raw-output0 '.[]' > $d/out printf '["a", "b"]' | $VALGRIND $Q $JQ --raw-output0 '.[]' > $d/out