Thu, 04 Oct 2012 18:23:32 +0200
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 |
1.1 --- a/test/map_tests.c Thu Oct 04 16:03:18 2012 +0200 1.2 +++ b/test/map_tests.c Thu Oct 04 18:23:32 2012 +0200 1.3 @@ -150,9 +150,9 @@ 1.4 FILE *f = fopen("test_ucx_map_store", "w"); 1.5 int r; 1.6 1.7 - fwrite(" # comment test", 1, 15, f); 1.8 + fwrite(" # comment test\n", 1, 16, f); 1.9 r = ucx_map_store(map, f); 1.10 - fwrite("#discard this", 1, 13, f); 1.11 + fwrite("!discard this", 1, 13, f); 1.12 1.13 fclose(f); 1.14 ucx_map_free(map); 1.15 @@ -165,20 +165,26 @@ 1.16 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 1.17 1.18 value = ucx_map_cstr_get(map, "test"); 1.19 + UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); 1.20 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); 1.21 1.22 value = ucx_map_cstr_get(map, "key"); 1.23 + UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); 1.24 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); 1.25 1.26 value = ucx_map_cstr_get(map, "other.very.long.key"); 1.27 + UCX_TEST_ASSERT(value != NULL, 1.28 + "value not found for key: other.very.long.key"); 1.29 UCX_TEST_ASSERT(strcmp(value, "value") == 0, 1.30 "value error for key: other.very.long.key"); 1.31 1.32 value = ucx_map_cstr_get(map, "testkey"); 1.33 + UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); 1.34 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, 1.35 "value error for key: testkey"); 1.36 1.37 value = ucx_map_cstr_get(map, "simple"); 1.38 + UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); 1.39 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " 1.40 "to test if the buffer extension works as designed") == 0, 1.41 "value error for key: simple");
2.1 --- a/ucx/map.c Thu Oct 04 16:03:18 2012 +0200 2.2 +++ b/ucx/map.c Thu Oct 04 18:23:32 2012 +0200 2.3 @@ -4,6 +4,9 @@ 2.4 2.5 #include <stdlib.h> 2.6 #include <string.h> 2.7 +#ifndef _WIN32 2.8 +#include <unistd.h> 2.9 +#endif /* not _WIN32 */ 2.10 2.11 #include "map.h" 2.12 2.13 @@ -192,13 +195,13 @@ 2.14 2.15 int ucx_map_load(UcxMap *map, FILE *f) { 2.16 2.17 - char c; int r, n; 2.18 + int c; int r, n; 2.19 2.20 char *key, *value; 2.21 2.22 - while ((c = (char) fgetc(f)) > 0) { 2.23 + while ((c = fgetc(f)) > 0) { 2.24 /* Discard leading spaces and comments */ 2.25 - if (c == ' ') continue; 2.26 + if (c < 33) continue; 2.27 if (c == '#' || c == '!') { 2.28 while ((c = (char) fgetc(f)) > 0) { 2.29 if (c == '\n') break; 2.30 @@ -218,28 +221,39 @@ 2.31 } 2.32 key[r] = c; 2.33 r++; 2.34 - } while ((c = (char) fgetc(f)) > 0); 2.35 - if (c == 0) { 2.36 + } while ((c = fgetc(f)) > 0); 2.37 + if (c <= 0) { 2.38 free(key); 2.39 return 1; 2.40 } 2.41 key[r] = 0; 2.42 + while (key[--r] == ' ') key[r] = 0; 2.43 + 2.44 + /* skip whitespaces */ 2.45 + while ((c = fgetc(f)) > 0) { 2.46 + if (c > 32) break; 2.47 + } 2.48 + if (c <= 0) { 2.49 + free(key); 2.50 + return 1; 2.51 + } 2.52 2.53 /* read into value buffer */ 2.54 n = 64; 2.55 value = malloc(n); 2.56 r = 0; 2.57 - while ((c = (char) fgetc(f)) > 0) { 2.58 + do { 2.59 if (c == '\n') break; 2.60 - if (r > n - 1) { 2.61 + if (r > n - 2) { 2.62 n *= 2; 2.63 value = realloc(value, n); 2.64 } 2.65 value[r] = c; 2.66 r++; 2.67 - } 2.68 - value = realloc(value, r+1); 2.69 + } while ((c = fgetc(f)) > 0); 2.70 value[r] = 0; 2.71 + while (value[--r] < 33) value[r] = 0; 2.72 + value = realloc(value, r+2); 2.73 2.74 ucx_map_cstr_put(map, key, value); 2.75 free(key);