Skip to content

Commit b5c4c3d

Browse files
itchynynicowilliams
authored andcommitted
Fix has(nan) on arrays to output false
1 parent 14e5e63 commit b5c4c3d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/jv_aux.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,14 @@ jv jv_has(jv t, jv k) {
214214
jv_free(elem);
215215
} else if (jv_get_kind(t) == JV_KIND_ARRAY &&
216216
jv_get_kind(k) == JV_KIND_NUMBER) {
217-
jv elem = jv_array_get(t, (int)jv_number_value(k));
218-
ret = jv_bool(jv_is_valid(elem));
217+
if (jvp_number_is_nan(k)) {
218+
ret = jv_false();
219+
} else {
220+
jv elem = jv_array_get(t, (int)jv_number_value(k));
221+
ret = jv_bool(jv_is_valid(elem));
222+
jv_free(elem);
223+
}
219224
jv_free(k);
220-
jv_free(elem);
221225
} else {
222226
ret = jv_invalid_with_msg(jv_string_fmt("Cannot check whether %s has a %s key",
223227
jv_kind_name(jv_get_kind(t)),

tests/jq.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,10 @@ map(has(2))
13891389
[[0,1], ["a","b","c"]]
13901390
[false, true]
13911391

1392+
has(nan)
1393+
[0,1,2]
1394+
false
1395+
13921396
keys
13931397
[42,3,35]
13941398
[0,1,2]

0 commit comments

Comments
 (0)