fixed crash fails by completing the implementation of the tested function....

Thu, 04 Oct 2012 18:23:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 18:23:32 +0200
changeset 43
02f38adea013
parent 42
ff3dd1ee7dee
child 44
46356d74e873

fixed crash fails by completing the implementation of the tested function....

test/map_tests.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
--- a/test/map_tests.c	Thu Oct 04 16:03:18 2012 +0200
+++ b/test/map_tests.c	Thu Oct 04 18:23:32 2012 +0200
@@ -150,9 +150,9 @@
     FILE *f = fopen("test_ucx_map_store", "w");
     int r;
 
-    fwrite(" # comment test", 1, 15, f);
+    fwrite(" # comment test\n", 1, 16, f);
     r = ucx_map_store(map, f);
-    fwrite("#discard this", 1, 13, f);
+    fwrite("!discard this", 1, 13, f);
 
     fclose(f);
     ucx_map_free(map);
@@ -165,20 +165,26 @@
     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
 
     value = ucx_map_cstr_get(map, "test");
+    UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
     UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
 
     value = ucx_map_cstr_get(map, "key");
+    UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
     UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
 
     value = ucx_map_cstr_get(map, "other.very.long.key");
+    UCX_TEST_ASSERT(value != NULL,
+            "value not found for key: other.very.long.key");
     UCX_TEST_ASSERT(strcmp(value, "value") == 0,
             "value error for key: other.very.long.key");
 
     value = ucx_map_cstr_get(map, "testkey");
+    UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
     UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
             "value error for key: testkey");
 
     value = ucx_map_cstr_get(map, "simple");
+    UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
     UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
             "to test if the buffer extension works as designed") == 0,
             "value error for key: simple");
--- a/ucx/map.c	Thu Oct 04 16:03:18 2012 +0200
+++ b/ucx/map.c	Thu Oct 04 18:23:32 2012 +0200
@@ -4,6 +4,9 @@
 
 #include <stdlib.h>
 #include <string.h>
+#ifndef _WIN32
+#include <unistd.h>
+#endif /* not _WIN32 */
 
 #include "map.h"
 
@@ -192,13 +195,13 @@
 
 int ucx_map_load(UcxMap *map, FILE *f) {
 
-    char c; int r, n;
+    int c; int r, n;
 
     char *key, *value;
 
-    while ((c = (char) fgetc(f)) > 0) {
+    while ((c = fgetc(f)) > 0) {
         /* Discard leading spaces and comments */
-        if (c == ' ') continue;
+        if (c < 33) continue;
         if (c == '#' || c == '!') {
             while ((c = (char) fgetc(f)) > 0) {
                 if (c == '\n') break;
@@ -218,28 +221,39 @@
             }
             key[r] = c;
             r++;
-        } while ((c = (char) fgetc(f)) > 0);
-        if (c == 0) {
+        } while ((c = fgetc(f)) > 0);
+        if (c <= 0) {
             free(key);
             return 1;
         }
         key[r] = 0;
+        while (key[--r] == ' ') key[r] = 0;
+
+        /* skip whitespaces */
+        while ((c = fgetc(f)) > 0) {
+            if (c > 32) break;
+        }
+        if (c <= 0) {
+            free(key);
+            return 1;
+        }
 
         /* read into value buffer */
         n = 64;
         value = malloc(n);
         r = 0;
-        while ((c = (char) fgetc(f)) > 0) {
+        do {
             if (c == '\n') break;
-            if (r > n - 1) {
+            if (r > n - 2) {
                 n *= 2;
                 value = realloc(value, n);
             }
             value[r] = c;
             r++;
-        }
-        value = realloc(value, r+1);
+        } while ((c = fgetc(f)) > 0);
         value[r] = 0;
+        while (value[--r] < 33) value[r] = 0;
+        value = realloc(value, r+2);
 
         ucx_map_cstr_put(map, key, value);
         free(key);

mercurial