Skip to content

Commit 359f0e2

Browse files
committed
fix: bsearch prevent overflow on mid calculation
Signed-off-by: Eloy Coto <[email protected]>
1 parent 95cbcc5 commit 359f0e2

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/builtin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ static jv f_bsearch(jq_state *jq, jv input, jv target) {
791791
int end = jv_array_length(jv_copy(input));
792792
jv answer = jv_invalid();
793793
while (start < end) {
794-
int mid = (start + end) / 2;
794+
int mid = start + (end - start) / 2;
795795
int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));
796796
if (result == 0) {
797797
answer = jv_number(mid);

tests/jq.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,10 @@ bsearch({x:1})
15591559
[{ "x": 0 },{ "x": 1 },{ "x": 2 }]
15601560
1
15611561

1562+
bsearch(0)
1563+
"aa"
1564+
jq: error (at <unknown>): string ("aa") cannot be searched from
1565+
15621566
# strptime tests are in optional.test
15631567

15641568
strftime("%Y-%m-%dT%H:%M:%SZ")

0 commit comments

Comments
 (0)