Skip to content

Commit

Permalink
Don't wrap negative indices twice (fix #2826)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Aug 6, 2023
1 parent a692060 commit 267585b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,19 +695,22 @@ jv jq_next(jq_state *jq) {
goto do_backtrack;
}
// $array | .[-1]
jv norm_k;
if (jv_get_kind(k) == JV_KIND_NUMBER && jv_get_kind(t) == JV_KIND_ARRAY) {
int idx = jv_number_value(k);
if (idx < 0) {
jv_free(k);
k = jv_number(jv_array_length(jv_copy(t)) + idx);
}
if (idx < 0)
norm_k = jv_number(jv_array_length(jv_copy(t)) + idx);
else
norm_k = jv_copy(k);
} else {
norm_k = jv_copy(k);
}
jv v = jv_get(t, jv_copy(k));
jv v = jv_get(t, k);
if (jv_is_valid(v)) {
path_append(jq, k, jv_copy(v));
path_append(jq, norm_k, jv_copy(v));
stack_push(jq, v);
} else {
jv_free(k);
jv_free(norm_k);
if (opcode == INDEX)
set_error(jq, v);
else
Expand Down
6 changes: 6 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2004,3 +2004,9 @@ walk(1)
walk(select(IN({}, []) | not))
{"a":1,"b":[]}
{"a":1}

# Issue #2826
.[-5]
[0,1,2]
null

0 comments on commit 267585b

Please sign in to comment.