Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This commit fixes a strange behavior of the combination of `--slurp` and `--stream` flags when the input has no trailing newline character.

authored by itchyny · committed by GitHub · parent 7d8c096 · Browse files · 2 files+11 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+11 added−12 removed

  • tests:+7 added−0 removed
  • other:+4 added−12 removed

2 files+11 added−12 removed

modified:src/util.c+4 added-12 removed
269 unmodified lines
270 } else {271 fclose(state->current_input);272 }273 state->current_input = NULL;274 jv_free(state->current_filename);275 state->current_filename = jv_invalid();276 state->current_line = 0 ;274 }275 const char *f = next_file(state);276 if (f != NULL) {277 jv_free(state->current_filename);278 state->current_line = 0;279 if (!strcmp(f, "-")) {280 state->current_input = stdin;281 state->current_filename = jv_string("<stdin>");282 } else {
3 unmodified lines
286 state->err_cb(state->err_cb_data, f);287 state->failures++;288 }289 }291 state->current_line = 0;290 }291 }292293 state->buf[0] = 0;
47 unmodified lines
341 state->buf_valid_len = (p - state->buf) + 1;342 }343 }344 }347 return state->curr_file == state->nfiles &&348 (!state->current_input || feof(state->current_input) || ferror(state->current_input));345 return state->curr_file == state->nfiles && !state->current_input;346}347348jv jq_util_input_next_input_cb(jq_state *jq, void *data) {349 return jq_util_input_next_input((jq_util_input_state *)data);
45 unmodified lines
395// Blocks to read one more input from stdin and/or given files396// When slurping, it returns just one value397jv jq_util_input_next_input(jq_util_input_state *state) {398 int is_last = 0;402 int has_more = 0;399 jv value = jv_invalid(); // need more input400 do {401 if (state->parser == NULL) {402 // Raw input
21 unmodified lines
424 jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_last);425 }426 value = jv_parser_next(state->parser);427 if (jv_is_valid(state->slurped)) {432 // When slurping an input that doesn't have a trailing newline,433 // we might have more than one value on the same line, so let's check434 // to see if we have more data to parse.435 has_more = jv_parser_remaining(state->parser);428 if (jv_is_valid(value)) {429 state->slurped = jv_array_append(state->slurped, value);430 value = jv_invalid();431 } else if (jv_invalid_has_msg(jv_copy(value)))
1 unmodified line
433 } else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) {434 return value;435 }436 }445 } while (!is_last || has_more);437 } while (!is_last);438439 if (jv_is_valid(state->slurped)) {440 value = state->slurped;441 state->slurped = jv_invalid();
815 unmodified lines
modified:tests/shtest+7 added
153 unmodified lines
154cmp $d/out $d/expected155printf "[1,2][3,4]\n" | $JQ -cs add > $d/out 2>&1156cmp $d/out $d/expected157158# Regression test for #3273159echo "[[[0],1],[[0]],[[0],2],[[0]]]" > $d/expected160printf "[1][2]" | $JQ -c -s --stream . > $d/out 2>&1161cmp $d/out $d/expected162printf "[1][2]\n" | $JQ -c -s --stream . > $d/out 2>&1163cmp $d/out $d/expected164165# Regression test for --raw-output0166printf "a\0b\0" > $d/expected167printf '["a", "b"]' | $VALGRIND $Q $JQ --raw-output0 '.[]' > $d/out168cmp $d/out $d/expected
616 unmodified lines