浏览代码

Fix frozen MSVC build

PUBLISHED_FROM=8772f8e95123ee88c346511e3d1ae8179afd4758
Sergey Lyubka 9 年之前
父节点
当前提交
3e54f537c7
共有 2 个文件被更改,包括 38 次插入25 次删除
  1. 29 20
      frozen.c
  2. 9 5
      unit_test.c

+ 29 - 20
frozen.c

@@ -282,20 +282,23 @@ static int parse_array(struct frozen *f) {
   TRY(test_and_skip(f, '['));
   {
     CALL_BACK(f, JSON_TYPE_ARRAY_START, NULL, 0);
-    SET_STATE(f, f->cur - 1, "", 0);
-    while (cur(f) != ']') {
-      snprintf(buf, sizeof(buf), "[%d]", i);
-      i++;
-      current_path_len = append_to_path(f, buf, strlen(buf));
-      f->cur_name = f->path + strlen(f->path) - strlen(buf) + 1/*opening brace*/;
-      f->cur_name_len = strlen(buf) - 2/*braces*/;
-      TRY(parse_value(f));
-      truncate_path(f, current_path_len);
-      if (cur(f) == ',') f->cur++;
+    {
+      SET_STATE(f, f->cur - 1, "", 0);
+      while (cur(f) != ']') {
+        snprintf(buf, sizeof(buf), "[%d]", i);
+        i++;
+        current_path_len = append_to_path(f, buf, strlen(buf));
+        f->cur_name = f->path + strlen(f->path) - strlen(buf) +
+          1 /*opening brace*/;
+        f->cur_name_len = strlen(buf) - 2 /*braces*/;
+        TRY(parse_value(f));
+        truncate_path(f, current_path_len);
+        if (cur(f) == ',') f->cur++;
+      }
+      TRY(test_and_skip(f, ']'));
+      truncate_path(f, fstate.path_len);
+      CALL_BACK(f, JSON_TYPE_ARRAY_END, fstate.ptr, f->cur - fstate.ptr);
     }
-    TRY(test_and_skip(f, ']'));
-    truncate_path(f, fstate.path_len);
-    CALL_BACK(f, JSON_TYPE_ARRAY_END, fstate.ptr, f->cur - fstate.ptr);
   }
   return 0;
 }
@@ -397,14 +400,16 @@ static int parse_object(struct frozen *f) {
   TRY(test_and_skip(f, '{'));
   {
     CALL_BACK(f, JSON_TYPE_OBJECT_START, NULL, 0);
-    SET_STATE(f, f->cur - 1, ".", 1);
-    while (cur(f) != '}') {
-      TRY(parse_pair(f));
-      if (cur(f) == ',') f->cur++;
+    {
+      SET_STATE(f, f->cur - 1, ".", 1);
+      while (cur(f) != '}') {
+        TRY(parse_pair(f));
+        if (cur(f) == ',') f->cur++;
+      }
+      TRY(test_and_skip(f, '}'));
+      truncate_path(f, fstate.path_len);
+      CALL_BACK(f, JSON_TYPE_OBJECT_END, fstate.ptr, f->cur - fstate.ptr);
     }
-    TRY(test_and_skip(f, '}'));
-    truncate_path(f, fstate.path_len);
-    CALL_BACK(f, JSON_TYPE_OBJECT_END, fstate.ptr, f->cur - fstate.ptr);
   }
   return 0;
 }
@@ -536,6 +541,10 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) {
 
         va_copy(sub_ap, ap);
         need_len = vsnprintf(buf, sizeof(buf), fmt2, sub_ap) + 1 /* null-term */;
+        /*
+         * TODO(lsm): Fix windows & eCos code path here. Their vsnprintf
+         * implementation returns -1 on overflow rather needed size.
+         */
         if (need_len > sizeof(buf)) {
           /*
            * resulting string doesn't fit into a stack-allocated buffer `buf`,

+ 9 - 5
unit_test.c

@@ -263,10 +263,11 @@ static const char *test_json_printf(void) {
 
   {
     struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    const char *result = "\"foo\"";
     out.u.buf.size = 6;
     memset(buf, 0, sizeof(buf));
     ASSERT(json_printf(&out, "%.*Q", 3, "foobar") == 5);
-    ASSERT(memcmp(buf, "\"foo\"", 5) == 0);
+    ASSERT(memcmp(buf, result, 5) == 0);
   }
 
   {
@@ -282,9 +283,10 @@ static const char *test_json_printf(void) {
 
   {
     struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    const char *fmt = "{a: \"%s\"}", *result = "{\"a\": \"b\"}";
     memset(buf, 0, sizeof(buf));
-    ASSERT(json_printf(&out, "{a: \"%s\"}", "b") > 0);
-    ASSERT(strcmp(buf, "{\"a\": \"b\"}") == 0);
+    ASSERT(json_printf(&out, fmt, "b") > 0);
+    ASSERT(strcmp(buf, result) == 0);
   }
 
   {
@@ -296,9 +298,10 @@ static const char *test_json_printf(void) {
 
   {
     struct json_out out = JSON_OUT_BUF(buf, sizeof(buf));
+    const char *result = "\"a_b0\": 1";
     memset(buf, 0, sizeof(buf));
     ASSERT(json_printf(&out, "a_b0: %d", 1) > 0);
-    ASSERT(strcmp(buf, "\"a_b0\": 1") == 0);
+    ASSERT(strcmp(buf, result) == 0);
   }
 
   return NULL;
@@ -398,8 +401,9 @@ static const char *test_scanf(void) {
   {
     /* Test errors */
     const char *str = "{foo:1, bar:[2,3,4]}";
+    size_t i;
     ASSERT(json_walk(str, strlen(str), NULL, NULL) == (int) strlen(str));
-    for (size_t i = 1; i < strlen(str); i++) {
+    for (i = 1; i < strlen(str); i++) {
       ASSERT(json_walk(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
     }
   }