Selaa lähdekoodia

Reset value to null instead of leaving intact

PUBLISHED_FROM=387ba1cd63d60caff0474e328f31ae7c6eb4657e
Dmitry Frank 8 vuotta sitten
vanhempi
commit
fc15e0c030
2 muutettua tiedostoa jossa 12 lisäystä ja 8 poistoa
  1. 11 7
      frozen.c
  2. 1 1
      unit_test.c

+ 11 - 7
frozen.c

@@ -868,7 +868,7 @@ static void json_scanf_cb(void *callback_data, const char *name,
     return;
   }
 
-  if (token->ptr == NULL || token->type == JSON_TYPE_NULL) {
+  if (token->ptr == 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
@@ -892,12 +892,16 @@ static void json_scanf_cb(void *callback_data, const char *name,
     }
     case 'Q': {
       char **dst = (char **) info->target;
-      int unescaped_len = json_unescape(token->ptr, token->len, NULL, 0);
-      if (unescaped_len >= 0 &&
-          (*dst = (char *) malloc(unescaped_len + 1)) != NULL) {
-        info->num_conversions++;
-        json_unescape(token->ptr, token->len, *dst, unescaped_len);
-        (*dst)[unescaped_len] = '\0';
+      if (token->type == JSON_TYPE_NULL) {
+        *dst = NULL;
+      } else {
+        int unescaped_len = json_unescape(token->ptr, token->len, NULL, 0);
+        if (unescaped_len >= 0 &&
+            (*dst = (char *) malloc(unescaped_len + 1)) != NULL) {
+          info->num_conversions++;
+          json_unescape(token->ptr, token->len, *dst, unescaped_len);
+          (*dst)[unescaped_len] = '\0';
+        }
       }
       break;
     }

+ 1 - 1
unit_test.c

@@ -549,7 +549,7 @@ static const char *test_scanf(void) {
 
   {
     const char *str = "{a : null }";
-    char *result = NULL;
+    char *result = (char *)123;
     ASSERT(json_scanf(str, strlen(str), "{a: %Q}", &result) == 0);
     ASSERT(result == NULL);
     free(result);