From ea7fa973cdd2e6ea5f4181e2191677b3a4df2ec9 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 2 Jun 2014 16:12:27 +0800 Subject: [PATCH] Add SAFE_FREE for char * errstr --- src/slug.c | 4 ++++ tests/check_tree.c | 33 +++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/slug.c b/src/slug.c index e3d2caa..a55c2fe 100644 --- a/src/slug.c +++ b/src/slug.c @@ -70,6 +70,10 @@ char * r3_slug_to_str(const r3_slug_t *s) { r3_slug_t * r3_slug_parse_next(r3_slug_t *s, char **errstr) { return r3_slug_parse(s->end, s->path_len - (s->end - s->begin), errstr); } + +Return 0 => Empty, slug not found +Return 1 => Slug found +Return -1 => Slug parsing error */ int r3_slug_parse(r3_slug_t *s, char *needle, int needle_len, char *offset, char **errstr) { diff --git a/tests/check_tree.c b/tests/check_tree.c index d376d50..40f1955 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -8,10 +8,11 @@ #include "zmalloc.h" #include "bench.h" - +#define SAFE_FREE(ptr) if(ptr) free(ptr); START_TEST (test_find_common_prefix) { + char *errstr = NULL; node * n = r3_tree_create(10); edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL); r3_node_append_edge(n,e); @@ -19,45 +20,61 @@ START_TEST (test_find_common_prefix) int prefix_len; edge *ret_edge = NULL; + + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/foo", sizeof("/foo")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/foo", sizeof("/foo")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 4); + SAFE_FREE(errstr); + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/foo/", sizeof("/foo/")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/foo/", sizeof("/foo/")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 5); + SAFE_FREE(errstr); + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/foo/{slog}", sizeof("/foo/{slog}")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/foo/{slog}", sizeof("/foo/{slog}")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 5); + SAFE_FREE(errstr); + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/foo/{bar}", sizeof("/foo/{bar}")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/foo/{bar}", sizeof("/foo/{bar}")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 5); + SAFE_FREE(errstr); + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/foo/bar", sizeof("/foo/bar")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/foo/bar", sizeof("/foo/bar")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 5); + SAFE_FREE(errstr); + + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "/bar/", sizeof("/bar/")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "/bar/", sizeof("/bar/")-1, &prefix_len, &errstr); ck_assert(ret_edge != NULL); ck_assert_int_eq(prefix_len, 1); + SAFE_FREE(errstr); + errstr = NULL; prefix_len = 0; - ret_edge = r3_node_find_common_prefix(n, "{bar}", sizeof("{bar}")-1, &prefix_len, NULL); + ret_edge = r3_node_find_common_prefix(n, "{bar}", sizeof("{bar}")-1, &prefix_len, &errstr); ck_assert(!ret_edge != NULL); ck_assert_int_eq(prefix_len, 0); + SAFE_FREE(errstr); r3_tree_free(n);