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:
Yo-An Lin 2018-02-21 23:01:03 +08:00 committed by GitHub
commit 2b37680087
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 37 deletions

View file

@ -51,7 +51,7 @@ int main()
match_entry * e = match_entry_createl("/garply/baz/grault", strlen("/garply/baz/grault") );
m = r3_tree_match_entry(n , e);
if (m) {
printf("Matched! %s\n", e->path);
printf("Matched! %s\n", e->path.base);
}
match_entry_free(e);
r3_tree_free(n);

View file

@ -74,7 +74,7 @@ typedef struct st_r3_buffer_prototype_t r3_buffer_prototype_t;
* buffer structure compatible with iovec
*/
typedef struct st_r3_iovec_t {
char *base;
const char *base;
unsigned int len;
} r3_iovec_t;
@ -147,7 +147,7 @@ struct st_r3_buffer_prototype_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

View file

@ -13,11 +13,11 @@ extern "C" {
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);

View file

@ -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(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);

View file

@ -64,7 +64,7 @@ R3Node * r3_edge_branch(R3Edge *e, int dl) {
R3Edge * new_edge;
// the rest string
char * s1 = e->pattern.base + dl;
const char * s1 = e->pattern.base + dl;
int s1_len = e->pattern.len - dl;
// the suffix edge of the leaf

View file

@ -19,6 +19,8 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <config.h>
#include <assert.h>
#include <errno.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;
};
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};

View file

@ -28,8 +28,8 @@
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
static int strndiff(char * d1, char * d2, unsigned int n) {
char * o = d1;
static int strndiff(const char * d1, const char * d2, unsigned int n) {
const char * o = d1;
while ( *d1 == *d2 && n-- > 0 ) {
d1++;
d2++;
@ -144,7 +144,7 @@ int r3_tree_compile(R3Node *n, char **errstr)
int ret = 0;
// bool use_slug = 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;
}
} else {
@ -422,7 +422,7 @@ R3Node * r3_tree_matchl(const R3Node * n, const char * path, unsigned int path_l
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;
if (!restlen) {
return e->child && e->child->endpoint ? e->child : NULL;
@ -505,7 +505,7 @@ void r3_route_free(R3Route * route) {
// 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_vector_reserve(NULL, &route->slugs, route->slugs.size + 1);
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) {
char *plh = (char*)path;
const char *plh = path;
unsigned int l, namel;
l = 0;
char *name;
const char *name;
while (plh < (path + path_len)) {
plh = r3_slug_find_placeholder(plh+l, path_len, &l);
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);
if ( slug_cnt > 1 ) {
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
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"
R3Node * child = r3_tree_create(3);
unsigned int paln = p - path;
r3_node_connectl(n, path, p - path, 0, child); // no duplicate
// 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) {
// there is one slug, let's see if it's optimiz-able by opcode
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;
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;
// if there is a pattern defined.

View file

@ -15,7 +15,7 @@
#include "slug.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++) {
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;
}
char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len) {
char *c;
char *s2;
const char * r3_slug_find_placeholder(const char *s1, unsigned int str_len, unsigned int *len) {
const char *c;
const char *s2;
int cnt = 0;
if (c = strnchr(s1, str_len, '{')) {
if ((c = strnchr(s1, str_len, '{'))) {
// find closing '}'
s2 = c;
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
*/
char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len) {
char *c;
char *s2;
const char * r3_slug_find_pattern(const char *s1, unsigned int str_len, unsigned int *len) {
const char *c;
const char *s2;
unsigned int cnt = 1;
if ( (c = strnchr(s1, str_len, ':')) ) {
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
*/
char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len) {
char * c;
char * s2;
int cnt = 0;
const char * r3_slug_find_name(const char *s1, unsigned int str_len, unsigned int *len) {
const char * c;
const char * s2;
unsigned int plholder;
if (c = r3_slug_find_placeholder(s1, str_len, &plholder)) {
if ((c = r3_slug_find_placeholder(s1, str_len, &plholder))) {
c++;
if ( s2 = strnchr(c, plholder, ':') ) {
if (( s2 = strnchr(c, plholder, ':') )) {
*len = s2 - c;
return c;
} 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 *s1 = NULL, *o = NULL;
char *pat = NULL;
const char *s1 = NULL;
char *o = NULL;
const char *pat = NULL;
char sep = '/';

View file

@ -20,7 +20,7 @@ void str_array_free(str_array *l) {
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_reserve(NULL, tks, tks->size + 1);
r3_iovec_t *temp = tks->entries + tks->size++;