Merge pull request #107 from martijnotto/2.0
const correctness, branched assignment, unused variables, non-standard feature test, example UB, incorrect memset declaration
This commit is contained in:
commit
2b37680087
9 changed files with 38 additions and 37 deletions
|
@ -51,7 +51,7 @@ int main()
|
||||||
match_entry * e = match_entry_createl("/garply/baz/grault", strlen("/garply/baz/grault") );
|
match_entry * e = match_entry_createl("/garply/baz/grault", strlen("/garply/baz/grault") );
|
||||||
m = r3_tree_match_entry(n , e);
|
m = r3_tree_match_entry(n , e);
|
||||||
if (m) {
|
if (m) {
|
||||||
printf("Matched! %s\n", e->path);
|
printf("Matched! %s\n", e->path.base);
|
||||||
}
|
}
|
||||||
match_entry_free(e);
|
match_entry_free(e);
|
||||||
r3_tree_free(n);
|
r3_tree_free(n);
|
||||||
|
|
|
@ -74,7 +74,7 @@ typedef struct st_r3_buffer_prototype_t r3_buffer_prototype_t;
|
||||||
* buffer structure compatible with iovec
|
* buffer structure compatible with iovec
|
||||||
*/
|
*/
|
||||||
typedef struct st_r3_iovec_t {
|
typedef struct st_r3_iovec_t {
|
||||||
char *base;
|
const char *base;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
} r3_iovec_t;
|
} r3_iovec_t;
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ struct st_r3_buffer_prototype_t {
|
||||||
|
|
||||||
typedef R3_VECTOR(void) r3_vector_t;
|
typedef R3_VECTOR(void) r3_vector_t;
|
||||||
|
|
||||||
extern void *(*r3_mem__set_secure)(void *, int, unsigned int);
|
extern void *(*r3_mem__set_secure)(void *, int, size_t);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* prints an error message and aborts
|
* prints an error message and aborts
|
||||||
|
|
|
@ -13,11 +13,11 @@ extern "C" {
|
||||||
|
|
||||||
char * r3_slug_compile(const char * str, unsigned int len);
|
char * r3_slug_compile(const char * str, unsigned int len);
|
||||||
|
|
||||||
char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len);
|
const char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len);
|
||||||
|
|
||||||
char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len);
|
const char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len);
|
||||||
|
|
||||||
char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len);
|
const char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len);
|
||||||
|
|
||||||
int r3_slug_count(const char * needle, int len, char **errstr);
|
int r3_slug_count(const char * needle, int len, char **errstr);
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ bool str_array_slugs_full(const str_array * l);
|
||||||
|
|
||||||
// bool str_array_append_slug(str_array * l, char * slug);
|
// bool str_array_append_slug(str_array * l, char * slug);
|
||||||
|
|
||||||
bool str_array_append(str_array * l, char * token, unsigned int len);
|
bool str_array_append(str_array * l, const char * token, unsigned int len);
|
||||||
|
|
||||||
void str_array_free(str_array *l);
|
void str_array_free(str_array *l);
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,7 @@ R3Node * r3_edge_branch(R3Edge *e, int dl) {
|
||||||
R3Edge * new_edge;
|
R3Edge * new_edge;
|
||||||
|
|
||||||
// the rest string
|
// the rest string
|
||||||
char * s1 = e->pattern.base + dl;
|
const char * s1 = e->pattern.base + dl;
|
||||||
int s1_len = e->pattern.len - dl;
|
int s1_len = e->pattern.len - dl;
|
||||||
|
|
||||||
// the suffix edge of the leaf
|
// the suffix edge of the leaf
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
* IN THE SOFTWARE.
|
* IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -52,7 +54,7 @@ struct st_r3_mem_pool_shared_ref_t {
|
||||||
struct st_r3_mem_pool_shared_entry_t *entry;
|
struct st_r3_mem_pool_shared_entry_t *entry;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *(*r3_mem__set_secure)(void *, int, unsigned int) = memset;
|
void *(*r3_mem__set_secure)(void *, int, size_t) = memset;
|
||||||
|
|
||||||
static __thread r3_mem_recycle_t mempool_allocator = {16};
|
static __thread r3_mem_recycle_t mempool_allocator = {16};
|
||||||
|
|
||||||
|
|
21
src/node.c
21
src/node.c
|
@ -28,8 +28,8 @@
|
||||||
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
|
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
|
||||||
|
|
||||||
|
|
||||||
static int strndiff(char * d1, char * d2, unsigned int n) {
|
static int strndiff(const char * d1, const char * d2, unsigned int n) {
|
||||||
char * o = d1;
|
const char * o = d1;
|
||||||
while ( *d1 == *d2 && n-- > 0 ) {
|
while ( *d1 == *d2 && n-- > 0 ) {
|
||||||
d1++;
|
d1++;
|
||||||
d2++;
|
d2++;
|
||||||
|
@ -144,7 +144,7 @@ int r3_tree_compile(R3Node *n, char **errstr)
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
// bool use_slug = r3_node_has_slug_edges(n);
|
// bool use_slug = r3_node_has_slug_edges(n);
|
||||||
if ( r3_node_has_slug_edges(n) ) {
|
if ( r3_node_has_slug_edges(n) ) {
|
||||||
if ( ret = r3_tree_compile_patterns(n, errstr) ) {
|
if (( ret = r3_tree_compile_patterns(n, errstr) )) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -422,7 +422,7 @@ R3Node * r3_tree_matchl(const R3Node * n, const char * path, unsigned int path_l
|
||||||
|
|
||||||
info("COMPARE COMPARE_STR\n");
|
info("COMPARE COMPARE_STR\n");
|
||||||
|
|
||||||
if (e = r3_node_find_edge_str(n, path, path_len)) {
|
if ((e = r3_node_find_edge_str(n, path, path_len))) {
|
||||||
restlen = path_len - e->pattern.len;
|
restlen = path_len - e->pattern.len;
|
||||||
if (!restlen) {
|
if (!restlen) {
|
||||||
return e->child && e->child->endpoint ? e->child : NULL;
|
return e->child && e->child->endpoint ? e->child : NULL;
|
||||||
|
@ -505,7 +505,7 @@ void r3_route_free(R3Route * route) {
|
||||||
// return route->slugs != NULL;
|
// return route->slugs != NULL;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
static r3_iovec_t* router_append_slug(R3Route * route, char * slug, unsigned int len) {
|
static r3_iovec_t* router_append_slug(R3Route * route, const char * slug, unsigned int len) {
|
||||||
r3_iovec_t *temp;
|
r3_iovec_t *temp;
|
||||||
r3_vector_reserve(NULL, &route->slugs, route->slugs.size + 1);
|
r3_vector_reserve(NULL, &route->slugs, route->slugs.size + 1);
|
||||||
temp = route->slugs.entries + route->slugs.size++;
|
temp = route->slugs.entries + route->slugs.size++;
|
||||||
|
@ -515,10 +515,10 @@ static r3_iovec_t* router_append_slug(R3Route * route, char * slug, unsigned int
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_slugs(R3Route * route, const char * path, int path_len) {
|
static void get_slugs(R3Route * route, const char * path, int path_len) {
|
||||||
char *plh = (char*)path;
|
const char *plh = path;
|
||||||
unsigned int l, namel;
|
unsigned int l, namel;
|
||||||
l = 0;
|
l = 0;
|
||||||
char *name;
|
const char *name;
|
||||||
while (plh < (path + path_len)) {
|
while (plh < (path + path_len)) {
|
||||||
plh = r3_slug_find_placeholder(plh+l, path_len, &l);
|
plh = r3_slug_find_placeholder(plh+l, path_len, &l);
|
||||||
if (!plh) break;
|
if (!plh) break;
|
||||||
|
@ -681,7 +681,7 @@ R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int pa
|
||||||
info("slug_cnt: %d\n",slug_cnt);
|
info("slug_cnt: %d\n",slug_cnt);
|
||||||
if ( slug_cnt > 1 ) {
|
if ( slug_cnt > 1 ) {
|
||||||
unsigned int slug_len;
|
unsigned int slug_len;
|
||||||
char *p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
const char *p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
assert(p);
|
assert(p);
|
||||||
|
@ -697,7 +697,6 @@ R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int pa
|
||||||
|
|
||||||
// insert the first one edge, and break at "p"
|
// insert the first one edge, and break at "p"
|
||||||
R3Node * child = r3_tree_create(3);
|
R3Node * child = r3_tree_create(3);
|
||||||
unsigned int paln = p - path;
|
|
||||||
r3_node_connectl(n, path, p - path, 0, child); // no duplicate
|
r3_node_connectl(n, path, p - path, 0, child); // no duplicate
|
||||||
|
|
||||||
// and insert the rest part to the child
|
// and insert the rest part to the child
|
||||||
|
@ -707,9 +706,9 @@ R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int pa
|
||||||
if (slug_cnt == 1) {
|
if (slug_cnt == 1) {
|
||||||
// there is one slug, let's see if it's optimiz-able by opcode
|
// there is one slug, let's see if it's optimiz-able by opcode
|
||||||
unsigned int slug_len = 0;
|
unsigned int slug_len = 0;
|
||||||
char *slug_p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
const char *slug_p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
||||||
unsigned int slug_pattern_len = 0;
|
unsigned int slug_pattern_len = 0;
|
||||||
char *slug_pattern = r3_slug_find_pattern(slug_p, slug_len, &slug_pattern_len);
|
const char *slug_pattern = r3_slug_find_pattern(slug_p, slug_len, &slug_pattern_len);
|
||||||
|
|
||||||
int opcode = 0;
|
int opcode = 0;
|
||||||
// if there is a pattern defined.
|
// if there is a pattern defined.
|
||||||
|
|
32
src/str.c
32
src/str.c
|
@ -15,7 +15,7 @@
|
||||||
#include "slug.h"
|
#include "slug.h"
|
||||||
#include "zmalloc.h"
|
#include "zmalloc.h"
|
||||||
|
|
||||||
static char * strnchr(const char* str, unsigned int len, int ch) {
|
static const char * strnchr(const char* str, unsigned int len, int ch) {
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
if (str[i] == ch) return str + i;
|
if (str[i] == ch) return str + i;
|
||||||
}
|
}
|
||||||
|
@ -86,11 +86,11 @@ char * r3_inside_slug(const char * needle, int needle_len, char *offset, char **
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len) {
|
const char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len) {
|
||||||
char *c;
|
const char *c;
|
||||||
char *s2;
|
const char *s2;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
if (c = strnchr(s1, str_len, '{')) {
|
if ((c = strnchr(s1, str_len, '{'))) {
|
||||||
// find closing '}'
|
// find closing '}'
|
||||||
s2 = c;
|
s2 = c;
|
||||||
unsigned int j = str_len - (c - s1);
|
unsigned int j = str_len - (c - s1);
|
||||||
|
@ -119,9 +119,9 @@ char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned i
|
||||||
/**
|
/**
|
||||||
* given a slug string, duplicate the pattern string of the slug
|
* given a slug string, duplicate the pattern string of the slug
|
||||||
*/
|
*/
|
||||||
char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len) {
|
const char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len) {
|
||||||
char *c;
|
const char *c;
|
||||||
char *s2;
|
const char *s2;
|
||||||
unsigned int cnt = 1;
|
unsigned int cnt = 1;
|
||||||
if ( (c = strnchr(s1, str_len, ':')) ) {
|
if ( (c = strnchr(s1, str_len, ':')) ) {
|
||||||
c++;
|
c++;
|
||||||
|
@ -152,14 +152,13 @@ char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *
|
||||||
/**
|
/**
|
||||||
* given a slug string, duplicate the parameter name string of the slug
|
* given a slug string, duplicate the parameter name string of the slug
|
||||||
*/
|
*/
|
||||||
char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len) {
|
const char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len) {
|
||||||
char * c;
|
const char * c;
|
||||||
char * s2;
|
const char * s2;
|
||||||
int cnt = 0;
|
|
||||||
unsigned int plholder;
|
unsigned int plholder;
|
||||||
if (c = r3_slug_find_placeholder(s1, str_len, &plholder)) {
|
if ((c = r3_slug_find_placeholder(s1, str_len, &plholder))) {
|
||||||
c++;
|
c++;
|
||||||
if ( s2 = strnchr(c, plholder, ':') ) {
|
if (( s2 = strnchr(c, plholder, ':') )) {
|
||||||
*len = s2 - c;
|
*len = s2 - c;
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
|
@ -177,8 +176,9 @@ char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len
|
||||||
*/
|
*/
|
||||||
char * r3_slug_compile(const char * str, unsigned int len)
|
char * r3_slug_compile(const char * str, unsigned int len)
|
||||||
{
|
{
|
||||||
char *s1 = NULL, *o = NULL;
|
const char *s1 = NULL;
|
||||||
char *pat = NULL;
|
char *o = NULL;
|
||||||
|
const char *pat = NULL;
|
||||||
char sep = '/';
|
char sep = '/';
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ void str_array_free(str_array *l) {
|
||||||
free(l->tokens.entries);
|
free(l->tokens.entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool str_array_append(str_array * l, char * token, unsigned int len) {
|
bool str_array_append(str_array * l, const char * token, unsigned int len) {
|
||||||
R3_VECTOR(r3_iovec_t) *tks = &l->tokens;
|
R3_VECTOR(r3_iovec_t) *tks = &l->tokens;
|
||||||
r3_vector_reserve(NULL, tks, tks->size + 1);
|
r3_vector_reserve(NULL, tks, tks->size + 1);
|
||||||
r3_iovec_t *temp = tks->entries + tks->size++;
|
r3_iovec_t *temp = tks->entries + tks->size++;
|
||||||
|
|
Loading…
Reference in a new issue