Browse Source

Frozen: Fixes for minor bugs found by Coverity

PUBLISHED_FROM=a84853cf318ecf9e720747af84967eb55f0d7674
Deomid Ryabkov 7 years ago
parent
commit
c739b4bcb8
3 changed files with 21 additions and 10 deletions
  1. 4 0
      Makefile
  2. 13 8
      frozen.c
  3. 4 2
      unit_test.c

+ 4 - 0
Makefile

@@ -23,5 +23,9 @@ vc98 vc2017:
 	$(RD) docker.cesanta.com/$@ wine cl unit_test.c $(CLFLAGS) /[email protected]
 	$(RD) docker.cesanta.com/$@ wine cl unit_test.c $(CLFLAGS) /[email protected]
 	$(RD) docker.cesanta.com/$@ wine [email protected] 
 	$(RD) docker.cesanta.com/$@ wine [email protected] 
 
 
+coverity: clean
+	rm -rf cov-int
+	nice cov-build --dir cov-int $(MAKE) c GCC= COVERITY=1
+
 clean:
 clean:
 	rm -rf *.gc* *.dSYM unit_test *.exe *.obj _CL_*
 	rm -rf *.gc* *.dSYM unit_test *.exe *.obj _CL_*

+ 13 - 8
frozen.c

@@ -673,7 +673,6 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) {
         if ((n + 1 == strlen("%" PRId64) && strcmp(fmt2, "%" PRId64) == 0) ||
         if ((n + 1 == strlen("%" PRId64) && strcmp(fmt2, "%" PRId64) == 0) ||
             (n + 1 == strlen("%" PRIu64) && strcmp(fmt2, "%" PRIu64) == 0)) {
             (n + 1 == strlen("%" PRIu64) && strcmp(fmt2, "%" PRIu64) == 0)) {
           (void) va_arg(ap, int64_t);
           (void) va_arg(ap, int64_t);
-          skip += strlen(PRIu64) - 1;
         } else if (strcmp(fmt2, "%.*s") == 0) {
         } else if (strcmp(fmt2, "%.*s") == 0) {
           (void) va_arg(ap, int);
           (void) va_arg(ap, int);
           (void) va_arg(ap, char *);
           (void) va_arg(ap, char *);
@@ -922,8 +921,13 @@ static void json_scanf_cb(void *callback_data, const char *name,
         if (unescaped_len >= 0 &&
         if (unescaped_len >= 0 &&
             (*dst = (char *) malloc(unescaped_len + 1)) != NULL) {
             (*dst = (char *) malloc(unescaped_len + 1)) != NULL) {
           info->num_conversions++;
           info->num_conversions++;
-          json_unescape(token->ptr, token->len, *dst, unescaped_len);
-          (*dst)[unescaped_len] = '\0';
+          if (json_unescape(token->ptr, token->len, *dst, unescaped_len) ==
+              unescaped_len) {
+            (*dst)[unescaped_len] = '\0';
+          } else {
+            free(*dst);
+            *dst = NULL;
+          }
         }
         }
       }
       }
       break;
       break;
@@ -1059,15 +1063,16 @@ char *json_fread(const char *path) {
   } else if (fseek(fp, 0, SEEK_END) != 0) {
   } else if (fseek(fp, 0, SEEK_END) != 0) {
     fclose(fp);
     fclose(fp);
   } else {
   } else {
-    size_t size = ftell(fp);
+    long size = ftell(fp);
     data = (char *) malloc(size + 1);
     data = (char *) malloc(size + 1);
-    if (data != NULL) {
+    if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
       fseek(fp, 0, SEEK_SET); /* Some platforms might not have rewind(), Oo */
       fseek(fp, 0, SEEK_SET); /* Some platforms might not have rewind(), Oo */
-      if (fread(data, 1, size, fp) != size) {
+      if (fread(data, 1, size, fp) != (size_t) size) {
         free(data);
         free(data);
-        return NULL;
+        data = NULL;
+      } else {
+        data[size] = '\0';
       }
       }
-      data[size] = '\0';
     }
     }
     fclose(fp);
     fclose(fp);
   }
   }

+ 4 - 2
unit_test.c

@@ -687,6 +687,7 @@ static const char *test_eos(void) {
   size_t n = 999;
   size_t n = 999;
   char *buf = (char *) malloc(n);
   char *buf = (char *) malloc(n);
   int s_len = strlen(s), a = 0;
   int s_len = strlen(s), a = 0;
+  ASSERT(buf != NULL);
   memset(buf, 'x', n);
   memset(buf, 'x', n);
   memcpy(buf, s, s_len);
   memcpy(buf, s, s_len);
   ASSERT(json_scanf(buf, n, "{a:%d}", &a) == 1);
   ASSERT(json_scanf(buf, n, "{a:%d}", &a) == 1);
@@ -708,7 +709,8 @@ static const char *test_fprintf(void) {
   const char *result = "{\"a\":123}\n";
   const char *result = "{\"a\":123}\n";
   char *p;
   char *p;
   ASSERT(json_fprintf(fname, "{a:%d}", 123) > 0);
   ASSERT(json_fprintf(fname, "{a:%d}", 123) > 0);
-  ASSERT((p = json_fread(fname)) != NULL);
+  p = json_fread(fname);
+  ASSERT(p != NULL);
   ASSERT(strcmp(p, result) == 0);
   ASSERT(strcmp(p, result) == 0);
   free(p);
   free(p);
   remove(fname);
   remove(fname);
@@ -877,7 +879,7 @@ static const char *test_prettify(void) {
     json_fprintf(fname, "{a:%d}", 123);
     json_fprintf(fname, "{a:%d}", 123);
     ASSERT(json_prettify_file(fname) > 0);
     ASSERT(json_prettify_file(fname) > 0);
     ASSERT(compare_file(fname, s));
     ASSERT(compare_file(fname, s));
-    remove(fname);
+    (void) remove(fname);
   }
   }
 
 
   return NULL;
   return NULL;