diff --git a/mongoose.c b/mongoose.c index 6c69d381..20c7b306 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2494,12 +2494,12 @@ int mg_json_get(const char *s, int len, const char *path, int *toklen) { // In the ascii table, the distance between `[` and `]` is 2. // Ditto for `{` and `}`. Hence +2 in the code below. -#define MG_EOO(x) \ - do { \ - if (depth == ed && ci != ei) return MG_JSON_NOT_FOUND; \ - if (c != nesting[depth - 1] + 2) return MG_JSON_INVALID; \ - depth--; \ - MG_CHECKRET(x); \ +#define MG_EOO(x) \ + do { \ + if (depth == ed && (ci != ei || ci < 0)) return MG_JSON_NOT_FOUND; \ + if (c != nesting[depth - 1] + 2) return MG_JSON_INVALID; \ + depth--; \ + MG_CHECKRET(x); \ } while (0) for (i = 0; i < len; i++) { diff --git a/src/json.c b/src/json.c index 4c960647..a0a04979 100644 --- a/src/json.c +++ b/src/json.c @@ -58,12 +58,12 @@ int mg_json_get(const char *s, int len, const char *path, int *toklen) { // In the ascii table, the distance between `[` and `]` is 2. // Ditto for `{` and `}`. Hence +2 in the code below. -#define MG_EOO(x) \ - do { \ - if (depth == ed && ci != ei) return MG_JSON_NOT_FOUND; \ - if (c != nesting[depth - 1] + 2) return MG_JSON_INVALID; \ - depth--; \ - MG_CHECKRET(x); \ +#define MG_EOO(x) \ + do { \ + if (depth == ed && (ci != ei || ci < 0)) return MG_JSON_NOT_FOUND; \ + if (c != nesting[depth - 1] + 2) return MG_JSON_INVALID; \ + depth--; \ + MG_CHECKRET(x); \ } while (0) for (i = 0; i < len; i++) { diff --git a/test/unit_test.c b/test/unit_test.c index b724ca92..8f0acaca 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -2317,7 +2317,7 @@ static void test_get_header_var(void) { static void test_json(void) { const char *s; const char *s1 = "{\"a\":{},\"b\":7,\"c\":[[],2]}"; - const char *s2 = "{\"a\":{\"b1\":{}},\"c\":7}"; + const char *s2 = "{\"a\":{\"b1\":{}},\"c\":7,\"d\":{\"b2\":{}}}"; int n, n1 = (int) strlen(s1), n2 = (int) strlen(s2); ASSERT(mg_json_get(" true ", 6, "", &n) == MG_JSON_INVALID);