Add SAFE_FREE for char * errstr

This commit is contained in:
c9s 2014-06-02 16:12:27 +08:00
parent 15021b11c9
commit ea7fa973cd
2 changed files with 29 additions and 8 deletions

View file

@ -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) { 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 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) { int r3_slug_parse(r3_slug_t *s, char *needle, int needle_len, char *offset, char **errstr) {

View file

@ -8,10 +8,11 @@
#include "zmalloc.h" #include "zmalloc.h"
#include "bench.h" #include "bench.h"
#define SAFE_FREE(ptr) if(ptr) free(ptr);
START_TEST (test_find_common_prefix) START_TEST (test_find_common_prefix)
{ {
char *errstr = NULL;
node * n = r3_tree_create(10); node * n = r3_tree_create(10);
edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL); edge * e = r3_edge_createl(zstrdup("/foo/{slug}"), sizeof("/foo/{slug}")-1, NULL);
r3_node_append_edge(n,e); r3_node_append_edge(n,e);
@ -19,45 +20,61 @@ START_TEST (test_find_common_prefix)
int prefix_len; int prefix_len;
edge *ret_edge = NULL; edge *ret_edge = NULL;
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 4); ck_assert_int_eq(prefix_len, 4);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 5); ck_assert_int_eq(prefix_len, 5);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 5); ck_assert_int_eq(prefix_len, 5);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 5); ck_assert_int_eq(prefix_len, 5);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 5); ck_assert_int_eq(prefix_len, 5);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(ret_edge != NULL);
ck_assert_int_eq(prefix_len, 1); ck_assert_int_eq(prefix_len, 1);
SAFE_FREE(errstr);
errstr = NULL;
prefix_len = 0; 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(!ret_edge != NULL);
ck_assert_int_eq(prefix_len, 0); ck_assert_int_eq(prefix_len, 0);
SAFE_FREE(errstr);
r3_tree_free(n); r3_tree_free(n);