Changeset: 0c596b077c76 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/0c596b077c76
Added Files:
sql/test/json/Tests/8063-json-filter-index-overflow.test
Modified Files:
monetdb5/modules/atoms/json.c
sql/test/json/Tests/All
Branch: default
Log Message:
fixed issue # 8063. ie check for out of range
diffs (153 lines):
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -12,6 +12,7 @@
* (c) 2013 Martin Kersten
*/
#include "monetdb_config.h"
+#include <errno.h>
#include "gdk.h"
#include "mal.h"
#include "mal_client.h"
@@ -716,8 +717,9 @@ typedef struct {
path_token token; /* Token kind */
char *name; /* specific key */
size_t namelen; /* the size of the key */
- int index; /* if index == INT_MAX
we got [*] */
- int first, last;
+ bool any; /* no explicit index,
or [*] */
+ lng index; /* explicit array index
*/
+ lng first, last;
} pattern;
static str
@@ -729,9 +731,10 @@ JSONcompile(Client ctx, const char *expr
for (s = expr; *s; s++) {
terms[t].token = CHILD_STEP;
- terms[t].index = INT_MAX;
- terms[t].first = INT_MAX;
- terms[t].last = INT_MAX;
+ terms[t].any = true;
+ terms[t].index = 0;
+ terms[t].first = 0;
+ terms[t].last = 0;
if (*s == '$') {
if (t && terms[t - 1].token != END_STEP)
throw(MAL, "json.compile", "Root node must be
first");
@@ -774,8 +777,14 @@ JSONcompile(Client ctx, const char *expr
if (*s != '*') {
/* TODO: Implement [start:step:end] indexing */
if (isdigit((unsigned char) *s)) {
- terms[t].index = atoi(s);
- terms[t].first = terms[t].last =
atoi(s);
+ long long val;
+
+ errno = 0;
+ val = strtoll(s, NULL, 10);
+ if (errno == ERANGE || val < (long
long) GDK_lng_min || val > (long long) GDK_lng_max)
+ throw(MAL, "json.path", "index
out of range");
+ terms[t].any = false;
+ terms[t].index = terms[t].first =
terms[t].last = (lng) val;
} else
throw(MAL, "json.path", "'*' or digit
expected");
}
@@ -875,7 +884,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
}
cnt = 0;
for (i = jt->elm[ji].next; i && cnt >= 0; i = jt->elm[i].next,
cnt++) {
- if (terms[ti].index == INT_MAX
+ if (terms[ti].any
|| (cnt >= terms[ti].first && cnt <=
terms[ti].last)) {
if (terms[ti].token == ANY_STEP) {
if (jt->elm[i].child)
@@ -892,7 +901,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
if (r == NULL)
r = (str) -1;
} else {
- r = JSONmatch(ma, jt, jt->elm[i].child,
terms, ti + 1, terms[ti].index == INT_MAX);
+ r = JSONmatch(ma, jt, jt->elm[i].child,
terms, ti + 1, terms[ti].any);
}
if (r == (str) -1 || r == (str) -2) {
return r;
@@ -909,7 +918,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
&& strncmp(terms[ti].name, jt->elm[i].value,
terms[ti].namelen) ==
0) || terms[ti].name == 0
|| terms[ti].name[0] == '*') {
- if (terms[ti].index == INT_MAX
+ if (terms[ti].any
|| (cnt >= terms[ti].first && cnt <=
terms[ti].last)) {
if (ti + 1 == MAXTERMS)
return NULL;
@@ -918,7 +927,7 @@ JSONmatch(allocator *ma, JSON *jt, int j
if (r == NULL)
r = (str) -1;
} else {
- r = JSONmatch(ma, jt,
jt->elm[i].child, terms, ti + 1, terms[ti].index == INT_MAX);
+ r = JSONmatch(ma, jt,
jt->elm[i].child, terms, ti + 1, terms[ti].any);
}
if (r == (str) -1 || r == (str) -2) {
return r;
@@ -1021,8 +1030,8 @@ JSONfilterInternal(Client ctx, json *ret
// pattern contains the .. operator
if (terms[i].token == ANY_STEP ||
// pattern contains the [*] operator
- (terms[i].token == CHILD_STEP && terms[i].index ==
INT_MAX && terms[i].name == NULL) ||
- (terms[i].token == CHILD_STEP && terms[i].index ==
INT_MAX && *terms[i].name == '*')) {
+ (terms[i].token == CHILD_STEP && terms[i].any &&
terms[i].name == NULL) ||
+ (terms[i].token == CHILD_STEP && terms[i].any &&
*terms[i].name == '*')) {
return_array = true;
break;
diff --git a/sql/test/json/Tests/8063-json-filter-index-overflow.test
b/sql/test/json/Tests/8063-json-filter-index-overflow.test
new file mode 100644
--- /dev/null
+++ b/sql/test/json/Tests/8063-json-filter-index-overflow.test
@@ -0,0 +1,39 @@
+query T
+SELECT json.filter(JSON '[10,20,30]', 1)
+----
+20
+
+# indexes that do not fit in a 32-bit int must not wrap around
+query T
+SELECT json.filter(JSON '[10,20,30]', 4294967297)
+----
+[]
+
+query T
+SELECT json.filter(JSON '[10,20,30]', 4294967296)
+----
+[]
+
+# INT_MAX is a valid array index, not the [*] wildcard
+query T
+SELECT json.filter(JSON '[10,20,30]', 2147483647)
+----
+[]
+
+query T
+SELECT json.filter(JSON '[10,20,30]', '$[4294967297]')
+----
+[]
+
+query T
+SELECT json.filter(JSON '{"book":[{"author":"Nigel Rees"},{"author":"Evelyn
Waugh"}]}', '.book.[4294967296]')
+----
+[]
+
+query T
+SELECT json.filter(JSON '[10,20,30]', '$[*]')
+----
+[10,20,30]
+
+statement error /index out of range/
+SELECT json.filter(JSON '[10,20,30]', '$[9223372036854775808]')
diff --git a/sql/test/json/Tests/All b/sql/test/json/Tests/All
--- a/sql/test/json/Tests/All
+++ b/sql/test/json/Tests/All
@@ -19,3 +19,4 @@ jsontext.Bug-6859
jsonnumber.Bug-6932
jsonparser.Bug-6943
jsonutf16.Bug-7576
+8063-json-filter-index-overflow
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]