mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
add checking of numeric indices to an array to see if they can reasonably be considered integers. Avoid undefined behaviour if out of bounds
This commit is contained in:
12
jv.c
12
jv.c
@@ -5,6 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "jv_alloc.h"
|
||||
#include "jv.h"
|
||||
@@ -140,6 +141,17 @@ double jv_number_value(jv j) {
|
||||
return j.u.number;
|
||||
}
|
||||
|
||||
int jv_is_integer(jv j){
|
||||
if(jv_get_kind(j) != JV_KIND_NUMBER){
|
||||
return 0;
|
||||
}
|
||||
double x = jv_number_value(j);
|
||||
if(x != x || x > INT_MAX || x < INT_MIN){
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x == (int)x;
|
||||
}
|
||||
|
||||
/*
|
||||
* Arrays (internal helpers)
|
||||
|
1
jv.h
1
jv.h
@@ -60,6 +60,7 @@ jv jv_bool(int);
|
||||
|
||||
jv jv_number(double);
|
||||
double jv_number_value(jv);
|
||||
int jv_is_integer(jv);
|
||||
|
||||
jv jv_array();
|
||||
jv jv_array_sized(int);
|
||||
|
13
jv_aux.c
13
jv_aux.c
@@ -51,10 +51,15 @@ jv jv_get(jv t, jv k) {
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
|
||||
// FIXME: don't do lookup for noninteger index
|
||||
v = jv_array_get(t, (int)jv_number_value(k));
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
if(jv_is_integer(k)){
|
||||
v = jv_array_get(t, (int)jv_number_value(k));
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
v = jv_null();
|
||||
}
|
||||
} else {
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_OBJECT) {
|
||||
|
@@ -718,3 +718,7 @@ map(has(2))
|
||||
keys
|
||||
[42,3,35]
|
||||
[0,1,2]
|
||||
|
||||
[][.]
|
||||
1000000000000000000
|
||||
null
|
||||
|
Reference in New Issue
Block a user