Count the line a value ends on when its newline has not been read yet (#3624)
`current_line` was incremented only when a read contained a newline, so `input_line_number` and error locations reported the previous line for a value whose line had not been terminated yet: the last line of input without a trailing newline, or a value ending exactly on the read-buffer boundary. Track whether the last read stopped short of a newline and add it to the reported line number.
This commit is contained in:
1 parent
78ff109e04
commit
5cd5edb2e9
2 files changed
+67
-3
No files matched your search
+11
-3
@@ -194,6 +194,9 @@ struct jq_util_input_state {
|
||||
jv current_filename;
|
||||
size_t current_line;
|
||||
int at_file_start;
|
||||
// Set when the last read stopped short of a newline, i.e. the next value
|
||||
// parsed out of the buffer sits on the line after current_line.
|
||||
int mid_line;
|
||||
};
|
||||
|
||||
static void fprinter(void *data, const char *fname) {
|
||||
@@ -279,6 +282,7 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
|
||||
jv_free(state->current_filename);
|
||||
state->current_line = 0;
|
||||
state->at_file_start = 1;
|
||||
state->mid_line = 0;
|
||||
if (!strcmp(f, "-")) {
|
||||
state->current_input = stdin;
|
||||
state->current_filename = jv_string("<stdin>");
|
||||
@@ -311,8 +315,12 @@ static int jq_util_input_read_more(jq_util_input_state *state) {
|
||||
} else {
|
||||
const char *p = memchr(state->buf, '\n', max_gets_len);
|
||||
|
||||
if (p != NULL)
|
||||
if (p != NULL) {
|
||||
state->current_line++;
|
||||
state->mid_line = 0;
|
||||
} else {
|
||||
state->mid_line = 1;
|
||||
}
|
||||
|
||||
if (p == NULL && feof(state->current_input)) {
|
||||
size_t i;
|
||||
@@ -377,7 +385,7 @@ jv jq_util_input_get_position(jq_state *jq) {
|
||||
if (jv_get_kind(s->current_filename) != JV_KIND_STRING)
|
||||
return jv_string("<unknown>");
|
||||
|
||||
jv v = jv_string_fmt("%s:%lu", jv_string_value(s->current_filename), (unsigned long)s->current_line);
|
||||
jv v = jv_string_fmt("%s:%lu", jv_string_value(s->current_filename), (unsigned long)(s->current_line + s->mid_line));
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -399,7 +407,7 @@ jv jq_util_input_get_current_line(jq_state* jq) {
|
||||
if (cb != jq_util_input_next_input_cb)
|
||||
return jv_invalid_with_msg(jv_string("Unknown input line number"));
|
||||
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
||||
jv v = jv_number(s->current_line);
|
||||
jv v = jv_number(s->current_line + s->mid_line);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@@ -439,6 +439,62 @@ if [ "$EC" -ne 2 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## Regression tests for issues #2242 and #2244 input_line_number
|
||||
## The counter only advanced when a read happened to land on a newline, so a
|
||||
## value on a line whose newline had not been read yet reported the line
|
||||
## before it: the last line of input without a trailing newline, and any value
|
||||
## ending exactly on the read-buffer boundary.
|
||||
|
||||
printf '1\n2\n3\n' > $d/input
|
||||
printf '1\n2\n3\n' > $d/expected
|
||||
$VALGRIND $Q $JQ -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# no trailing newline on the last line (#2244)
|
||||
printf '1\n2\n3' > $d/input
|
||||
printf '1\n2\n3\n' > $d/expected
|
||||
$VALGRIND $Q $JQ -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# a single value with no trailing newline is still on line 1
|
||||
printf '0' > $d/input
|
||||
echo 1 > $d/expected
|
||||
$VALGRIND $Q $JQ -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# raw input, no trailing newline
|
||||
printf 'a\nb\nc' > $d/input
|
||||
printf '1\n2\n3\n' > $d/expected
|
||||
$VALGRIND $Q $JQ -R -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# a value ending exactly on the read-buffer boundary (#2242). The lengths
|
||||
# sweep a few bytes around the boundary of the 4096-byte read buffer in
|
||||
# src/util.c; update them if the buffer size changes.
|
||||
for len in 4087 4088 4089 4090 4091 4092 4093; do
|
||||
awk -v n=$len 'BEGIN{printf "0\n\""; while (n-- > 0) printf "a"; printf "\"\n0\n"}' > $d/input
|
||||
printf '1\n2\n3\n' > $d/expected
|
||||
$VALGRIND $Q $JQ -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
done
|
||||
|
||||
# several values on one line all report that line
|
||||
printf '1 2 3\n' > $d/input
|
||||
printf '1\n1\n1\n' > $d/expected
|
||||
$VALGRIND $Q $JQ -c input_line_number $d/input > $d/out
|
||||
cmp $d/out $d/expected
|
||||
|
||||
# error locations report the same line as input_line_number
|
||||
cat > $d/expected <<'EOF'
|
||||
jq: error (at <stdin>:1): Cannot index number with string ("a")
|
||||
jq: error (at <stdin>:2): Cannot index number with string ("a")
|
||||
EOF
|
||||
if printf '1\n2' | $VALGRIND $Q $JQ .a > /dev/null 2> $d/out; then
|
||||
echo "jq succeeded unexpectedly" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
cmp $d/out $d/expected
|
||||
|
||||
## Fuzz parser
|
||||
|
||||
## XXX With a $(urandom) builtin we could move this test into tests/all.test
|
||||
|
||||
Reference in new issue
Block a user