Merge pull request #1697 from oakfire/master

fix: #1696  mg_json_get() bugs.
This commit is contained in:
Sergey Lyubka 2022-08-31 17:55:12 +01:00 committed by GitHub
commit 5d2c977029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -2517,10 +2517,15 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
MG_DBGP('-');
switch (expecting) {
case S_VALUE:
if ( depth == ed && ei == ci && c != '[' && path[pos] == '[' ) return MG_JSON_NOT_FOUND;
if (depth == ed) j = i;
if (c == '{') {
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;
expecting = S_KEY;
break;
@ -2537,6 +2542,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
nesting[depth++] = c;
break;
} else if (c == ']' && depth > 0) { // Empty array
if (depth == ed) ci = -1;
MG_EOO(']');
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
i += 3;
@ -2556,7 +2562,11 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
return MG_JSON_INVALID;
}
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;
break;
@ -2602,7 +2612,10 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
// MG_CHECKRET('C');
} else if (c == ']' || c == '}') {
MG_EOO('O');
if (depth == ed && ei >= 0) ci++;
if (depth == ed && ei >= 0) {
ci++;
if (ci > ei) return MG_JSON_NOT_FOUND;
}
} else {
return MG_JSON_INVALID;
}

View File

@ -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, "$.ab", -42) == 2);
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);
}
}