diff options
author | FreeArtMan <dos21h@gmail.com> | 2015-12-18 21:13:02 +0000 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2015-12-18 21:13:02 +0000 |
commit | 2441dad315cdf050995e5b1fb48bf9e871fe791c (patch) | |
tree | 4d689a86e048a3727b42600d075c5dac512258e3 | |
parent | 4c058db338ff00e0e97cea0e985625deb47143cd (diff) | |
download | dm-2441dad315cdf050995e5b1fb48bf9e871fe791c.tar.gz dm-2441dad315cdf050995e5b1fb48bf9e871fe791c.zip |
Added assertions all over the code. Fixed missing () in realloc size calc part that caused mem violation problem
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | dm.c | 28 | ||||
-rw-r--r-- | syntax.c | 148 | ||||
-rw-r--r-- | syntax.h | 5 | ||||
-rw-r--r-- | tokenizer.c | 25 |
5 files changed, 192 insertions, 21 deletions
@@ -1,6 +1,6 @@ PROJECT=dm CC=gcc -CFLAGS=-std=c99 +CFLAGS= SOURCES=$(PROJECT)_parser.c darray.c tokenizer.c syntax.c mtable.c OBJECTS=$(SOURCES:.c=.o) @@ -18,3 +18,8 @@ ragel: clean: rm -f $(PROJECT) rm -f *.o + +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./dm -f test/test.bin -m test/one_byte.dm + #valgrind --track-origins=yes --log-file=log.txt ./dm -f test/test.bin -m test/one_byte.dm + @@ -9,21 +9,35 @@ #include "syntax.h" #include "mtable.h" +/* options avaliable */ +#define ASSERT_ON +#define __EXIT_ABBORT +#include "assert.h" + /*****************************************************************************/ int ast2tb( ast_root *ast, mt_table *mt ) { + AS_NULL(ast) + AS_NULL(mt) + int i,j,k; ast_range *arng = NULL; mt_range *mrng = NULL; for (i=0;i<ast->total_tokens;i++) { - arng = ast->range[i]; + arng = (ast_range *)ast->range[i]; + + AS_NULL(arng) + //AS_NULL(arng->val) + //AS_NULL(arng->iff) - if ((arng->val != NULL) && (arng->iff == NULL)) + if (arng->val != NULL) + if (arng->iff == NULL) { mrng = malloc(sizeof(mt_range)); + AS_NULL(mrng) memset(mrng,0,sizeof(mt_range)); mrng->start = arng->start; @@ -43,6 +57,7 @@ int ast2tb( ast_root *ast, mt_table *mt ) if (arng->iff[j]->expr->size_cmp > 0) { mrng = malloc(sizeof(mt_range)); + AS_NULL(mrng) memset(mrng,0,sizeof(mt_range)); //set start end positions @@ -50,6 +65,7 @@ int ast2tb( ast_root *ast, mt_table *mt ) mrng->end = arng->end; cmp = malloc(arng->iff[j]->expr->size_cmp); + AS_NULL(cmp) for (k=0;k<arng->iff[j]->expr->size_cmp;k++) cmp[k] = arng->iff[j]->expr->cmp[k]->val; mrng->cmp = cmp; @@ -74,6 +90,9 @@ int ast2tb( ast_root *ast, mt_table *mt ) * maycome */ int match( mt_table *mt, char *buf, size_t size ) { + AS_NULL(mt) + AS_NULL(buf) + //anonymouse function int pr(char *str, int sz) { @@ -243,12 +262,13 @@ int main(int argc, char **argv) ast = ast_syntax( tl ); //printf("ast = 0x%08x\n", ast); - //print_ast( ast ); + print_ast( ast ); ast2tb( ast, mt ); - //mt_print( mt ); + mt_print( mt ); match( mt, binbuf, binbuf_size ); + //ast_destroy( ast ); tl_destroy( tl ); mt_destroy( mt ); @@ -1,5 +1,10 @@ #include "syntax.h" +/* options avaliable */ +#define ASSERT_ON +#define __EXIT_ABBORT +#include "assert.h" + //token postion/type #define TP(POS) tl_tok_type(tl,(POS)) #define TP_EQ(POS,TYPE) (tl_tok_type(tl,(POS)) == TYPE) @@ -14,10 +19,13 @@ /*****************************************************************************/ ast_root* ast_syntax( token_list *tl ) { + AS_NULL(tl) + ast_root *ast_tree = NULL; ast_range *rng = NULL; tk_pos=0; + ast_tree = atrt_i(); while ( tk_pos < tl_size(tl) ) @@ -27,15 +35,23 @@ ast_root* ast_syntax( token_list *tl ) && TP_NEQ(tk_pos+1,TOK_RANGE)) { rng = att_range( tl, tk_pos ); - if (rng == NULL ) break; - atrt_a_range( &ast_tree, rng, 0 ); + if (rng == NULL ) + { + ERROR("rng=null\n"); + break; + } + atrt_a_range( &ast_tree, rng ); // current - INT|HEX next RANGE } else if ( TP_EQ2(tk_pos,TOK_INT,TOK_HEX) && TP_EQ(tk_pos+1,TOK_RANGE) ) { rng = att_range( tl, tk_pos ); - if (rng == NULL ) break; - atrt_a_range( &ast_tree, rng, 0 ); + if (rng == NULL ) + { + ERROR("rng == NULL\n"); + break; + } + atrt_a_range( &ast_tree, rng ); } else { printf("ERR:%d\n",tl_tok_type(tl,tk_pos)); @@ -52,7 +68,8 @@ ast_root* ast_syntax( token_list *tl ) ast_if* att_if( token_list *tl, int pos ) { //PRINT("IF:pos->%d %d\n",tk_pos,pos); - + AS_NULL(tl) + AS_EXPR(pos<0) ast_if *ret = NULL; ast_expr *expr = NULL; @@ -89,6 +106,8 @@ ast_if* att_if( token_list *tl, int pos ) ast_range* att_range( token_list *tl, int pos ) { //PRINT("RNG:pos->%d %d\n",tk_pos,pos); + AS_NULL(tl) + ast_range *ret = NULL; ast_value *val = NULL; ast_if *iff = NULL; @@ -106,7 +125,11 @@ ast_range* att_range( token_list *tl, int pos ) ret = atrn_i(); atrn_s_rng0( &ret, tok2int(tl_get_tok(tl,pos)) ); val = att_value( tl, pos+1 ); - if (val==NULL) return NULL; + if (val==NULL) + { + ERROR("val == NULL\n"); + return NULL; + } atrn_s_val( &ret, val ); tk_pos = pos+2; //current HEX IF ... @@ -115,7 +138,11 @@ ast_range* att_range( token_list *tl, int pos ) ret = atrn_i(); atrn_s_rng0( &ret, tok2int(tl_get_tok(tl,pos))); iff = att_if( tl, pos+1 ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); //current HEX RNG HEX } else if (TP_EQ(pos+1,TOK_RANGE) && @@ -126,13 +153,21 @@ ast_range* att_range( token_list *tl, int pos ) if (TP_EQ(pos+3,TOK_STR)) { val = att_value(tl,pos+3); - if (val==NULL) return NULL; + if (val==NULL) + { + ERROR("val == NULL\n"); + return NULL; + } atrn_s_val( &ret, val ); tk_pos = pos+4; } else if (TP_EQ(pos+3,TOK_IF)) { iff = att_if( tl, pos+3 ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); } } else @@ -147,7 +182,11 @@ ast_range* att_range( token_list *tl, int pos ) pos = tk_pos; //PRINT("There is more ifs\n"); iff = att_if( tl, pos ); - if (iff==NULL) return NULL; + if (iff==NULL) + { + ERROR("iff == NULL\n"); + return NULL; + } atrn_a_if( &ret, iff ); //PNL(); } @@ -159,6 +198,7 @@ ast_range* att_range( token_list *tl, int pos ) /*****************************************************************************/ ast_expr* att_expr( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("EXPR:pos->%d %d\n",tk_pos,pos); ast_expr *ret = NULL; @@ -190,6 +230,7 @@ ast_expr* att_expr( token_list *tl, int pos ) /*****************************************************************************/ ast_expr_cmp* att_expr_cmp( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("CMP:pos->%d %d\n",tk_pos,pos); ast_expr_cmp *ret = NULL; @@ -212,12 +253,14 @@ ast_expr_cmp* att_expr_cmp( token_list *tl, int pos ) /*****************************************************************************/ ast_expr_bit* att_expr_bit( token_list *tl, int pos ) { + AS_NULL(tl) return NULL; } /*****************************************************************************/ ast_value* att_value( token_list *tl, int pos ) { + AS_NULL(tl) //PRINT("VAL:pos->%d %d\n", tk_pos, pos); ast_value *val = NULL; token *tok = NULL; @@ -252,15 +295,21 @@ ast_root* atrt_i() } memset( ret, 0, sizeof(ast_root) ); + + return ret; } /*****************************************************************************/ -int atrt_a_range( ast_root **rt, ast_range *rng, int idx) +int atrt_a_range( ast_root **rt, ast_range *rng) { - //PNL(); - (*rt)->range = realloc( (*rt)->range, sizeof(void*)*(*rt)->total_tokens+1 ); + AS_NULL(rt) + AS_NULL(*rt) + AS_NULL(rng) + + (*rt)->range = realloc( (*rt)->range, sizeof(void*)*((*rt)->total_tokens+1) ); + AS_NULL((*rt)->range) (*rt)->range[(*rt)->total_tokens] = rng; (*rt)->total_tokens += 1; @@ -288,8 +337,11 @@ ast_range* atrn_i() /*****************************************************************************/ int atrn_a_if( ast_range **rng, ast_if *iff ) { + AS_NULL(rng) + AS_NULL(iff) //PNL(); - (*rng)->iff = realloc( (*rng)->iff, sizeof(void*)*(*rng)->size_iff+1 ); + (*rng)->iff = realloc( (*rng)->iff, sizeof(void*)*((*rng)->size_iff+1) ); + AS_NULL((*rng)->iff) (*rng)->iff[(*rng)->size_iff] = iff; (*rng)->size_iff += 1; return 0; @@ -299,6 +351,10 @@ int atrn_a_if( ast_range **rng, ast_if *iff ) /*****************************************************************************/ int atrn_a_val( ast_range **rng, ast_value *val ) { + AS_NULL(rng) + AS_NULL(*rng) + AS_NULL(val) + (*rng)->val = val; return 0; @@ -308,6 +364,8 @@ int atrn_a_val( ast_range **rng, ast_value *val ) /*****************************************************************************/ int atrn_s_rng0( ast_range **rng, int value ) { + AS_NULL(rng) + (*rng)->range_type = 0; (*rng)->start = value; (*rng)->end = value; @@ -319,6 +377,9 @@ int atrn_s_rng0( ast_range **rng, int value ) /*****************************************************************************/ int atrn_s_rng1( ast_range **rng, unsigned long start, unsigned long end) { + AS_NULL(rng) + AS_EXPR(start>end) + (*rng)->range_type = 1; (*rng)->start = start; (*rng)->end = end; @@ -330,6 +391,10 @@ int atrn_s_rng1( ast_range **rng, unsigned long start, unsigned long end) /*****************************************************************************/ int atrn_s_val( ast_range **rng, ast_value *val ) { + AS_NULL(rng) + AS_NULL(*rng) + AS_NULL(val) + (*rng)->val = val; return 0; @@ -356,6 +421,9 @@ ast_if* ati_i() /*****************************************************************************/ int ati_s_expr( ast_if **iff, ast_expr *expr ) { + AS_NULL(iff) + AS_NULL(expr) + (*iff)->expr = expr; return -1; } @@ -364,6 +432,9 @@ int ati_s_expr( ast_if **iff, ast_expr *expr ) /*****************************************************************************/ int ati_s_val( ast_if **iff, ast_value *val ) { + AS_NULL(iff) + AS_NULL(val) + (*iff)->val = val; return -1; } @@ -389,15 +460,20 @@ ast_expr* ate_i() /*****************************************************************************/ int ate_a_bit( ast_expr **expr, ast_expr_bit *bit ) { + AS_NULL(expr) + AS_NULL(*expr) + AS_NULL(bit) //PNL(); if ((*expr)->bit == NULL) { (*expr)->bit = malloc(sizeof(void *)); + AS_NULL((*expr)->bit) (*expr)->bit[0] = bit; (*expr)->size_bit = 1; } else { (*expr)->bit = realloc( (*expr)->bit, sizeof(void*)*((*expr)->size_bit+1) ); + AS_NULL((*expr)->bit) (*expr)->bit[(*expr)->size_bit] = bit; (*expr)->size_bit += 1; } @@ -409,15 +485,19 @@ int ate_a_bit( ast_expr **expr, ast_expr_bit *bit ) /*****************************************************************************/ int ate_a_cmp( ast_expr **expr, ast_expr_cmp *cmp ) { + AS_NULL(expr) + AS_NULL(cmp) //PNL(); if ((*expr)->cmp == NULL) { (*expr)->cmp = malloc(sizeof(void *)); + AS_NULL((*expr)->cmp) (*expr)->cmp[0] = cmp; (*expr)->size_cmp = 1; } else { (*expr)->cmp = realloc( (*expr)->cmp, sizeof(void*)*((*expr)->size_cmp+1) ); + AS_NULL((*expr)->cmp) (*expr)->cmp[(*expr)->size_cmp] = cmp; (*expr)->size_cmp += 1; } @@ -429,6 +509,8 @@ int ate_a_cmp( ast_expr **expr, ast_expr_cmp *cmp ) /*****************************************************************************/ int ate_eval( ast_expr *expr ) { + AS_NULL(expr) + return -1; } @@ -453,6 +535,8 @@ ast_expr_bit* ateb_i() /*****************************************************************************/ int ateb_s_mask( ast_expr_bit **expr, unsigned char mask ) { + AS_NULL(expr) + (*expr)->mask = mask; return 0; @@ -479,7 +563,11 @@ ast_expr_cmp* atec_i() /*****************************************************************************/ int atec_s_val( ast_expr_cmp **expr, unsigned char val ) { + AS_NULL(expr) + AS_NULL(*expr) + (*expr)->val = val; + return 0; } @@ -504,6 +592,10 @@ ast_value* atv_i() /*****************************************************************************/ int atv_s_note( ast_value **val, char *note, int size) { + AS_NULL(val) + AS_NULL(note) + AS_EXPR(size<1) + (*val)->note = note; (*val)->size = size; @@ -513,6 +605,8 @@ int atv_s_note( ast_value **val, char *note, int size) /*****************************************************************************/ int print_ast( ast_root *root ) { + AS_NULL(root) + int i,j,k; ast_range *rng = NULL; @@ -526,6 +620,7 @@ int print_ast( ast_root *root ) { printf("RANGE:%d\n",i ); rng = root->range[i]; + AS_NULL(rng) if ( rng->range_type == 0 || rng->range_type == 1 ) { printf("%d-%d:\n",rng->start, rng->end); @@ -538,6 +633,7 @@ int print_ast( ast_root *root ) printf("\tIF\n"); for (j=0;j<rng->size_iff;j++) { + AS_NULL(rng->iff[j]) if (rng->iff[j]->expr != NULL) { printf("\t\tCMP:"); @@ -556,4 +652,28 @@ int print_ast( ast_root *root ) } } return 0; +} + +/*****************************************************************************/ +int ast_destroy( ast_root *root ) +{ + AS_NULL(root) + + int i,j,k; + ast_range *rng; + + + if (root == NULL) + return -1; + + for (i=0;i<root->total_tokens;i++) + { + rng = root->range[i]; + if (rng == NULL) continue; + + } + free( root->range ); + free( root ); + + return 0; }
\ No newline at end of file @@ -10,6 +10,8 @@ #include "debug.h" #include "tokenizer.h" + + #define ALLOC_MEMSET(T,VAL) {VAL=malloc(sizeof(T));memset(VAL,0,sizeof(T));} @@ -93,7 +95,7 @@ ast_expr_cmp* att_expr_cmp( token_list *tl, int pos ); ast_expr_bit* att_expr_bit( token_list *tl, int pos ); ast_value* att_value( token_list *tl, int pos ); ast_root* atrt_i(); -int atrt_a_range( ast_root **rt, ast_range *rng, int idx); +int atrt_a_range( ast_root **rt, ast_range *rng); ast_range* atrn_i(); int atrn_a_if( ast_range **rng, ast_if *iff ); int atrn_a_val( ast_range **rng, ast_value *val ); @@ -114,5 +116,6 @@ int atec_s_val( ast_expr_cmp **expr, unsigned char val ); ast_value* atv_i(); int atv_s_note( ast_value **val, char *note, int size); int print_ast( ast_root *root ); +int ast_destroy( ast_root *root ); #endif
\ No newline at end of file diff --git a/tokenizer.c b/tokenizer.c index f69d404..a7b3afb 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -1,5 +1,9 @@ #include "tokenizer.h" +/* options avaliable */ +#define ASSERT_ON +#define __EXIT_ABBORT +#include "assert.h" /*****************************************************************************/ token* token_create() @@ -14,6 +18,13 @@ token* token_create() /*****************************************************************************/ int token_set( token **tok, int val, char *s, char *e ) { + AS_NULL(s) + AS_NULL(e) + AS_NULL(tok) + AS_NULL(*tok) + AS_EXPR(s>e) + AS_EXPR(val<0) + if ( tok == NULL ) { printf("token is NULL\n"); @@ -64,6 +75,11 @@ error: /*****************************************************************************/ int tl_add_tok( token_list *tl, int t, char *s, char *e ) { + AS_NULL(tl) + AS_NULL(s) + AS_NULL(e) + AS_EXPR(t<1) + token *tok = NULL; tok = token_create(); if (tok == NULL) @@ -80,6 +96,7 @@ int tl_add_tok( token_list *tl, int t, char *s, char *e ) /*****************************************************************************/ char* tl_str( token_list *tl ) { + AS_NULL(tl) int i = 0; for (i=0; i<darr_end(tl->list); i++) @@ -105,18 +122,24 @@ void tl_destroy( token_list *tl ) { darr_clear_destroy( tl->list ); } + + free(tl); } /*****************************************************************************/ int tl_size( token_list *tl ) { + AS_NULL(tl) + return darr_end( tl->list ); } int tok2int( token *tok ) { + AS_NULL(tok) + char str[128]; int sz = tok->e - tok->s; memcpy(str,tok->s,sz); @@ -128,6 +151,6 @@ int tok2int( token *tok ) char *tok2str( token *tok ) { - + return NULL; } |