Browse Source

Check for NULL token type in scanf callback

PUBLISHED_FROM=4e5b3fad14e32343dffaeee7ad4c3c615792544e
John Goyette 8 years ago
parent
commit
6a99ab39a4
2 changed files with 10 additions and 2 deletions
  1. 1 1
      frozen.c
  2. 9 1
      unit_test.c

+ 1 - 1
frozen.c

@@ -868,7 +868,7 @@ static void json_scanf_cb(void *callback_data, const char *name,
     return;
   }
 
-  if (token->ptr == NULL) {
+  if (token->ptr == NULL || token->type == JSON_TYPE_NULL) {
     /*
      * We're not interested here in the events for which we have no value;
      * namely, JSON_TYPE_OBJECT_START and JSON_TYPE_ARRAY_START

+ 9 - 1
unit_test.c

@@ -355,7 +355,7 @@ static void cb(void *data, const char *name, size_t name_len, const char *path,
   sprintf(buf + strlen(buf), "name:'%.*s', path:'%s', type:%s, val:'%.*s'\n",
           (int) (name != NULL ? name_len : strlen(snull)),
           name != NULL ? name : snull, path, tok_type_names[token->type],
-          (int) (token->ptr != NULL ? token->len : strlen(snull)),
+          (int) (token->ptr != NULL ? token->len : (int)strlen(snull)),
           token->ptr != NULL ? token->ptr : snull);
 }
 
@@ -547,6 +547,14 @@ static const char *test_scanf(void) {
     free(result);
   }
 
+  {
+    const char *str = "{a : null }";
+    char *result = NULL;
+    ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 0);
+    ASSERT(result == NULL);
+    free(result);
+  }
+
   return NULL;
 }