gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH] json parser: fix empty array bug


From: chris
Subject: [gpsd-dev] [PATCH] json parser: fix empty array bug
Date: Tue, 12 Jun 2012 18:47:52 +0100

From: Christian Gagneraud <address@hidden>

This patch fix the last bug of my AIS campaign:
The JSON parser fails to parse an empty array of struct.

After trying to modify the state machine in json_read_object(), I realised, that
the simpliest and the most natural way was to check for this special case
before running the loop in json_read_array().

Eric, if you're OK with this patch, I'll push it with my previous ones, except
of course the hexdump one, which btw, am still waiting for your suggestions.

I could commit as well my AIS bug fixes and the updated testsuites, but
without the hexdump fix, the aivdm-regress will fail.

---
 json.c      |    7 +++++++
 test_json.c |    9 +++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/json.c b/json.c
index 3f3278f..5550f22 100644
--- a/json.c
+++ b/json.c
@@ -529,6 +529,13 @@ int json_read_array(const char *cp, const struct 
json_array_t *arr,
 
     tp = arr->arr.strings.store;
     arrcount = 0;
+
+    /* Check for empty array */
+    while (isspace(*cp))
+       cp++;
+    if (*cp == ']')
+       goto breakout;
+
     for (offset = 0; offset < arr->maxlen; offset++) {
        json_debug_trace((1, "Looking at %s\n", cp));
        switch (arr->element_type) {
diff --git a/test_json.c b/test_json.c
index 2c365e6..85059f8 100644
--- a/test_json.c
+++ b/test_json.c
@@ -206,6 +206,11 @@ static const struct json_attr_t json_attrs_8[] = {
     {"foe",  t_integer, .addr.integer = &foe, .map=enum_table},
     {NULL},
 };
+
+/* Case 9: Like case 6 but w/ an empty array */
+
+static const char *json_str9 = "{\"parts\":[]}";
+
 /*@ +fullinitblock @*/
 /* *INDENT-ON* */
 
@@ -303,6 +308,10 @@ int main(int argc UNUSED, char *argv[]UNUSED)
     assert_integer("fie", fie, 6);
     assert_integer("foe", foe, 14);
 
+    status = json_read_object(json_str9, json_attrs_6, NULL);
+    assert_case(9, status);
+    assert_integer("dumbcount", dumbcount, 0);
+
     (void)fprintf(stderr, "succeeded.\n");
 
     exit(0);
-- 
1.7.7




reply via email to

[Prev in Thread] Current Thread [Next in Thread]