mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-28 20:58:31 +00:00
fix: #1696 mg_json_get() bugs.
This commit is contained in:
1 parent
0a265e79a6
commit
a686beb9a7
2 files changed
+29
-3
No files matched your search
+16
-3
@@ -2517,10 +2517,15 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
MG_DBGP('-');
|
MG_DBGP('-');
|
||||||
switch (expecting) {
|
switch (expecting) {
|
||||||
case S_VALUE:
|
case S_VALUE:
|
||||||
|
if ( depth == ed && ei == ci && c != '[' && path[pos] == '[' ) return MG_JSON_NOT_FOUND;
|
||||||
if (depth == ed) j = i;
|
if (depth == ed) j = i;
|
||||||
if (c == '{') {
|
if (c == '{') {
|
||||||
if (depth >= (int) sizeof(nesting)) return MG_JSON_TOO_DEEP;
|
if (depth >= (int) sizeof(nesting)) return MG_JSON_TOO_DEEP;
|
||||||
if (depth == ed && path[pos] == '.') ed++, pos++;
|
if (depth == ed && (ci == ei || ci < 0) && path[pos] == '.') {
|
||||||
|
ed++, pos++;
|
||||||
|
ci = -1;
|
||||||
|
ei = -1;
|
||||||
|
}
|
||||||
nesting[depth++] = c;
|
nesting[depth++] = c;
|
||||||
expecting = S_KEY;
|
expecting = S_KEY;
|
||||||
break;
|
break;
|
||||||
@@ -2537,6 +2542,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
nesting[depth++] = c;
|
nesting[depth++] = c;
|
||||||
break;
|
break;
|
||||||
} else if (c == ']' && depth > 0) { // Empty array
|
} else if (c == ']' && depth > 0) { // Empty array
|
||||||
|
if (depth == ed) ci = -1;
|
||||||
MG_EOO(']');
|
MG_EOO(']');
|
||||||
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
|
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
|
||||||
i += 3;
|
i += 3;
|
||||||
@@ -2556,7 +2562,11 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
return MG_JSON_INVALID;
|
return MG_JSON_INVALID;
|
||||||
}
|
}
|
||||||
MG_CHECKRET('V');
|
MG_CHECKRET('V');
|
||||||
if (depth == ed && ei >= 0) ci++;
|
if (depth == ed && ei >= 0) {
|
||||||
|
ci++;
|
||||||
|
if (ci > ei) return MG_JSON_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
expecting = S_COMMA_OR_EOO;
|
expecting = S_COMMA_OR_EOO;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -2602,7 +2612,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
|||||||
// MG_CHECKRET('C');
|
// MG_CHECKRET('C');
|
||||||
} else if (c == ']' || c == '}') {
|
} else if (c == ']' || c == '}') {
|
||||||
MG_EOO('O');
|
MG_EOO('O');
|
||||||
if (depth == ed && ei >= 0) ci++;
|
if (depth == ed && ei >= 0) {
|
||||||
|
ci++;
|
||||||
|
if (ci > ei) return MG_JSON_NOT_FOUND;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return MG_JSON_INVALID;
|
return MG_JSON_INVALID;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2495,6 +2495,19 @@ static void test_json(void) {
|
|||||||
ASSERT(mg_json_get_long(json, "$.a[0]", -42) == 1);
|
ASSERT(mg_json_get_long(json, "$.a[0]", -42) == 1);
|
||||||
ASSERT(mg_json_get_long(json, "$.ab", -42) == 2);
|
ASSERT(mg_json_get_long(json, "$.ab", -42) == 2);
|
||||||
ASSERT(mg_json_get_long(json, "$.ac", -42) == -42);
|
ASSERT(mg_json_get_long(json, "$.ac", -42) == -42);
|
||||||
|
|
||||||
|
json = mg_str("{\"a\":[],\"b\":[1,2]}");
|
||||||
|
ASSERT(mg_json_get_long(json, "$.a[0]", -42) == -42);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.b[0]", -42) == 1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.b[1]", -42) == 2);
|
||||||
|
ASSERT(mg_json_get_long(json, "$.b[2]", -42) == -42);
|
||||||
|
json = mg_str("[{\"a\":1,\"b\":2},{\"a\":3, \"b\":4}]");
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0].a", -42) == 1);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[0].b", -42) == 2);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[1].a", -42) == 3);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[1].b", -42) == 4);
|
||||||
|
ASSERT(mg_json_get_long(json, "$[2].a", -42) == -42);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user