From a877a8ed7b508eb8cc5f7911dfcec8b8612470b3 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Fri, 2 Jun 2017 16:20:44 +0100 Subject: Fixed memleaks, rearranged some code --- Makefile | 2 +- netbytes.c | 123 ++++++++++++++++++++++++++++++++++++++++++-- netbytes.h | 64 +++++++++++++---------- test/Makefile | 8 ++- test/test_add_elem.c | 16 +++--- test/test_alltypes.c | 48 ++++++----------- test/test_key_value_store.c | 101 +++++++++++++++++++++++++++++++----- test/test_multiple_write.c | 25 +++++---- test/test_save.c | 44 +++++++++------- 9 files changed, 312 insertions(+), 119 deletions(-) diff --git a/Makefile b/Makefile index 0b097c1..8b93f99 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PROJ=libnetbytes CC=gcc -CFLAGS= +CFLAGS=-g3 LDFLAGS= SOURCE=netbytes diff --git a/netbytes.c b/netbytes.c index dca7d22..2bc34b7 100644 --- a/netbytes.c +++ b/netbytes.c @@ -117,14 +117,24 @@ int nb_init( netbyte_store *store ) int nb_add_u8( netbyte_store *store, nb_u8 *u8 ) { + nb_u8 *new_u8=NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) + { return -1; + } if ( u8->type != NBT_U8 ) + { return -2; + } store->types[store->count].type = NBT_U8; - store->types[store->count].nb_val = (uint8_t *)u8; + + new_u8 = malloc(sizeof(nb_u8)); + memcpy(new_u8, u8, sizeof(nb_u8)); + store->types[store->count].nb_val = (uint8_t *)new_u8; + store->count += 1; return 0; } @@ -132,6 +142,8 @@ int nb_add_u8( netbyte_store *store, nb_u8 *u8 ) int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) { + nb_u8arr *new_u8arr = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -139,7 +151,13 @@ int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) return -2; store->types[store->count].type = NBT_U8ARRAY; - store->types[store->count].nb_val = (uint8_t *)u8arr; + + new_u8arr = malloc(sizeof(nb_u8arr)); + memcpy(new_u8arr, u8arr, sizeof(nb_u8arr)); + new_u8arr->val = malloc(sizeof(nb_u8arr)*u8arr->len); + memcpy(new_u8arr->val, u8arr->val, u8arr->len); + store->types[store->count].nb_val = (uint8_t *)new_u8arr; + store->count += 1; return 0; } @@ -310,7 +328,9 @@ uint8_t *nb_create( netbyte_store *store ) /* create netbyte memory chunk */ buf = malloc( store->size ); if ( buf == NULL ) + { return NULL; + } c = buf; /* write size of netbytes */ @@ -409,6 +429,49 @@ uint8_t *nb_create( netbyte_store *store ) } +int nb_free(netbyte_store *store) +{ + int i; + + if (!store) + { + return -1; + } + + for (i=0;icount;i++) + { + __NBT_TYPED type = store->types[i].type; + uint8_t *val = store->types[i].nb_val; + + switch(type) + { + case NBT_U8: + { + nb_u8 *u8 = (nb_u8 *)val; + free(u8); + u8=NULL; + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } + case NBT_U8ARRAY: + { + nb_u8arr *u8arr = (nb_u8arr *)val; + free(u8arr->val); + free(u8arr); + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } + default: + printf("Unknown type\n"); + } + } + free(store); + + return 0; +} + int nb_load( netbyte_store *store, uint8_t *data ) { __NBT_TYPED type; @@ -417,10 +480,14 @@ int nb_load( netbyte_store *store, uint8_t *data ) int count; if ( store == NULL ) + { return -1; + } if ( data == NULL ) + { return -2; + } __NBT_SIZE size; memcpy( &size, c, sizeof(__NBT_SIZE) ); @@ -429,7 +496,7 @@ int nb_load( netbyte_store *store, uint8_t *data ) size -= sizeof(__NBT_SIZE); c += sizeof(__NBT_SIZE); - if ( c == data ) + if (c == data) { store->size = size; return 0; @@ -450,6 +517,8 @@ int nb_load( netbyte_store *store, uint8_t *data ) c += sizeof(nb_u8); nb_add_u8( store, u8 ); + + free(u8); break; } @@ -468,6 +537,8 @@ int nb_load( netbyte_store *store, uint8_t *data ) c += u8arr->len; nb_add_u8arr( store, u8arr ); + + free(u8arr); break; } @@ -578,6 +649,7 @@ int nb_count( netbyte_store *store ) int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ) { + __NBT_TYPED nbt; if ( store == NULL ) { return -1; @@ -588,8 +660,9 @@ int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ) return -2; } - *type = store->types[count].type; //warnign? who cares - + //nbt = (__NBT_TYPED)store->types[count].type; //warnign? who cares + //*type = nbt; + *type = store->types[count].type; return 0; } @@ -651,3 +724,43 @@ int nb_fread( netbyte_store *store, int fd) return 0; } + +int nb_print(netbyte_store *store) +{ + int i; + + if (!store) + { + printf("nb: NULL\n"); + return -1; + } + + printf("nb: size=0x%x count=0x%x\n", store->size, store->count); + + for (i=0;icount;i++) + { + printf("\t[%x] -> ", i); + switch (store->types[i].type) + { + case NBT_U8: + { + nb_u8 *u8 = (nb_u8 *)store->types[i].nb_val; + printf("u8: t=0x%x v=0x%x", + u8->type, u8->val); + break; + } + case NBT_U8ARRAY: + { + nb_u8arr *u8arr = (nb_u8arr *)store->types[i].nb_val; + printf("u8arr: t=0x%x l=0x%x v=0x%x", + u8arr->type, u8arr->len, u8arr->val); + break; + } + default: + printf("Unknown"); + } + printf("\n"); + } + + return 0; +} \ No newline at end of file diff --git a/netbytes.h b/netbytes.h index e4937fb..8b332b5 100644 --- a/netbytes.h +++ b/netbytes.h @@ -12,20 +12,20 @@ #define NBT_NONE 0x00 /* maybe not yet prepared type */ #define NBT_U8 0x01 /* single unsigned 8 bits */ #define NBT_U8ARRAY 0x02 /* u8 array */ -#define NBT_U16 0x03 /**/ -#define NBT_U16ARRAY 0x04 /**/ -#define NBT_U32 0x05 /**/ -#define NBT_U32ARRAY 0x06 /**/ -#define NBT_U64 0x07 /**/ -#define NBT_U64ARRAY 0x08 /**/ -#define NBT_I8 0x09 /**/ -#define NBT_I8ARRAY 0x0A /**/ -#define NBT_I16 0x0B /**/ -#define NBT_I16ARRAY 0x0C /**/ -#define NBT_I32 0x0D /**/ -#define NBT_I32ARRAY 0x0E /**/ -#define NBT_I64 0x0F /**/ -#define NBT_I64ARRAY 0x10 /**/ +#define NBT_U16 0x03 /* unsigned 16 bit */ +#define NBT_U16ARRAY 0x04 /* u16 array */ +#define NBT_U32 0x05 /* unsigned 32 bit */ +#define NBT_U32ARRAY 0x06 /* u32 array */ +#define NBT_U64 0x07 /* unsigned 64 bit */ +#define NBT_U64ARRAY 0x08 /* u64 array */ +#define NBT_I8 0x09 /* signed 8 bits */ +#define NBT_I8ARRAY 0x0A /* i8 array */ +#define NBT_I16 0x0B /* signed 16bits */ +#define NBT_I16ARRAY 0x0C /* i16 array */ +#define NBT_I32 0x0D /* signed 32 bits */ +#define NBT_I32ARRAY 0x0E /* i32 array */ +#define NBT_I64 0x0F /* signed 64 bits */ +#define NBT_I64ARRAY 0x10 /* i64 array */ #define NBT_F16 0x11 /**/ #define NBT_F32 0x12 /**/ #define NBT_F64 0x13 /**/ @@ -47,7 +47,7 @@ //creating netbyte structure typedef struct __nb_type { - __NBT_TYPED type; + __NBT_TYPED type; uint8_t *nb_val; } __nb_type; @@ -75,14 +75,14 @@ typedef struct nb_u8arr /*-----------------*/ typedef struct nb_u16 { - __NBT_TYPED type; + __NBT_TYPED type; uint16_t val; } nb_u16; typedef struct nb_u16arr { - __NBT_TYPED type; - __NBT_U16ARR_LEN len; + __NBT_TYPED type; + __NBT_U16ARR_LEN len; uint16_t *val; } nb_u16arr; @@ -90,14 +90,14 @@ typedef struct nb_u16arr /*-----------------*/ typedef struct nb_u32 { - __NBT_TYPED type; + __NBT_TYPED type; uint32_t val; } nb_u32; typedef struct nb_u32arr { - __NBT_TYPED type; - __NBT_U32ARR_LEN len; + __NBT_TYPED type; + __NBT_U32ARR_LEN len; uint32_t *val; } nb_u32arr; @@ -105,26 +105,26 @@ typedef struct nb_u32arr /*------------------*/ typedef struct nb_u64 { - __NBT_TYPED type; + __NBT_TYPED type; uint64_t val; } nb_u64; typedef struct nb_u64arr { - __NBT_TYPED type; - __NBT_U64ARR_LEN len; + __NBT_TYPED type; + __NBT_U64ARR_LEN len; uint64_t *val; } nb_u64arr; //loading/parsing netbyte structure typedef struct netbyte_load { - __NBT_SIZE size; + __NBT_SIZE size; uint8_t *buf; } netbyte_load; -int nb_u8_create( nb_u8 *s, uint8_t val ); -int nb_u8arr_create( nb_u8arr *s, __NBT_U8ARR_LEN len, uint8_t *val ); +int nb_u8_create( nb_u8 *s, uint8_t val ); +int nb_u8arr_create( nb_u8arr *s, __NBT_U8ARR_LEN len, uint8_t *val ); int nb_u16_create( nb_u16 *s, uint16_t val ); int nb_u16arr_create( nb_u16arr *s, __NBT_U16ARR_LEN len, uint16_t *val ); int nb_u32_create( nb_u32 *s, uint32_t val ); @@ -133,7 +133,6 @@ int nb_u64_create( nb_u64 *s, uint64_t val ); int nb_u64arr_create( nb_u64arr *s, __NBT_U64ARR_LEN len, uint64_t *val ); int nb_init( netbyte_store *store ); - int nb_add_u8( netbyte_store *store, nb_u8 *u8 ); int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ); int nb_add_u16( netbyte_store *store, nb_u16 *u16 ); @@ -142,7 +141,10 @@ int nb_add_u32( netbyte_store *store, nb_u32 *u32 ); int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr ); int nb_add_u64( netbyte_store *store, nb_u64 *u64 ); int nb_add_u64arr( netbyte_store *store, nb_u64arr *u64arr ); + uint8_t *nb_create( netbyte_store *store ); +int nb_free(netbyte_store *store); + int nb_load( netbyte_store *store, uint8_t *data ); int nb_count( netbyte_store *store ); @@ -150,4 +152,10 @@ int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ); int nb_val( netbyte_store *store, int count, uint8_t **val ); int nb_fread( netbyte_store *store, int fd); +//print all all values in netbyte string +int nb_print(netbyte_store *store); + +//check if netbyte matches particular format +int nb_match(netbyte_store *store, char *frmt); + #endif \ No newline at end of file diff --git a/test/Makefile b/test/Makefile index c4f4b28..c70c7f0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,11 +1,15 @@ CC=gcc -CFLAGS=-I../ -lnetbytes -Wl,-rpath=../ -L../ +CFLAGS=-g3 -I../ -lnetbytes -Wl,-rpath=../ -L../ LDFLAGS= -SOURCE=test_save test_multiple_write test_multiple_read test_add_elem test_alltypes +SOURCE=test_save test_multiple_write test_multiple_read test_add_elem test_alltypes \ +test_key_value_store test_key_value_load SOURCES=$(SOURCE:=.c) make: $(SOURCE) +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test_key_value_store + clean: rm -f $(SOURCE) \ No newline at end of file diff --git a/test/test_add_elem.c b/test/test_add_elem.c index 5244880..7a789d1 100644 --- a/test/test_add_elem.c +++ b/test/test_add_elem.c @@ -66,10 +66,10 @@ int main() nb_u8 id; nb_u8arr name; - netbyte_store nb; + netbyte_store *nb=malloc(sizeof(netbyte_store)); printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); printf("Your name : "); fflush( stdin ); n = fgets( str, STR_SIZE, stdin ); @@ -82,21 +82,23 @@ int main() printf("er create u8arr: %d\n",er); - er = nb_add_u8( &nb, &id ); + er = nb_add_u8( nb, &id ); if (er) printf("er add u8: %d\n",er); - er = nb_add_u8arr( &nb, &name ); + er = nb_add_u8arr( nb, &name ); if (er) printf("er add u8arr: %d\n",er); - pr_store( &nb ); - res = nb_create( &nb ); + pr_store( nb ); + res = nb_create( nb ); f = fopen("test_many.nb","a"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); + free(res); + nb_free(nb); printf("End test\n"); return 0; diff --git a/test/test_alltypes.c b/test/test_alltypes.c index 2975d2a..b6eb801 100644 --- a/test/test_alltypes.c +++ b/test/test_alltypes.c @@ -104,8 +104,7 @@ int main() { FILE *f=NULL; int er; - netbyte_store nb; - netbyte_store nb2; + netbyte_store *nb = malloc(sizeof(netbyte_store)); nb_u8 u8; nb_u8arr u8arr; nb_u16 u16; @@ -120,7 +119,7 @@ int main() printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); er = nb_u8_create( &u8, 0x10 ); if (er) printf("er create u8: %d\n",er); @@ -142,8 +141,8 @@ int main() pr_u8arr( &u8arr ); #if 1 - pr_store( &nb ); - er = nb_add_u8( &nb, &u8 ); + pr_store( nb ); + er = nb_add_u8( nb, &u8 ); if (er) printf("er add u8: %d\n",er); #endif @@ -156,54 +155,39 @@ int main() #endif #if 1 - pr_store( &nb ); - er = nb_add_u16( &nb, &u16 ); + pr_store( nb ); + er = nb_add_u16( nb, &u16 ); if (er) printf("er add u16: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u32( &nb, &u32 ); + pr_store( nb ); + er = nb_add_u32( nb, &u32 ); if (er) printf("er add u32: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u64( &nb, &u64 ); + pr_store( nb ); + er = nb_add_u64( nb, &u64 ); if (er) printf("er add u64: %d\n",er); #endif - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); printf("res 0x%x\n", res); f = fopen("test_alltypes.nb","w+"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); - /* - printf("LOAD:-----\n"); - nb_init( &nb2 ); - pr_store( &nb2 ); - nb_load( &nb2, res ); - pr_store( &nb2 ); - - printf( "->1 [%s]\n", ((nb_u8arr *)nb2.types[1].nb_val)->val ); - - __NBT_TYPED *t=0xf8; - nb_u8arr *v; - nb_type( &nb2, 1, &t ); - printf("get type: %02x\n", t ); - nb_val( &nb2, 1, (uint8_t **)&v ); - printf("get value: %02x\n", v->val ); - */ - free( res ); + nb_free(nb); + printf("End test\n"); return 0; diff --git a/test/test_key_value_store.c b/test/test_key_value_store.c index 983b3b8..b499c9a 100644 --- a/test/test_key_value_store.c +++ b/test/test_key_value_store.c @@ -3,6 +3,38 @@ #include +char *alloc_new_str_s(char *str, size_t size) +{ + char *ret = NULL; + + if (str == NULL) + { + return NULL; + } + + //1MB is enought + if (size > (1024*1024)) + { + return NULL; + } + + ret = malloc(size+1); //extra for 1 zero at then end + if (ret == NULL) + { + return NULL; + } + + memcpy(ret, str, size); + ret[size] = 0; //add zero at the end + + return ret; +} + +char *alloc_new_str(char *str) +{ + return alloc_new_str_s(str, strlen(str)); +} + typedef struct kv_user_name { int id; @@ -12,17 +44,51 @@ typedef struct kv_user_name kv_user_name* kv_new(int id, char *name, char *desc) { - + kv_user_name *ret = NULL; + + ret = malloc(sizeof(kv_user_name)); + if (!ret) + { + return NULL; + } + + ret->id = id; + ret->name = alloc_new_str(name); + ret->desc = alloc_new_str(desc); + + return ret; } netbyte_store* kv_marsh(kv_user_name *kv) { - return NULL; + netbyte_store *nb=NULL; + nb_u8 nb_id; + nb_u8arr nb_name, nb_desc; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8_create(&nb_id, kv->id); + nb_add_u8(nb, &nb_id); + nb_u8arr_create(&nb_name, strlen(kv->name), kv->name); + nb_add_u8arr(nb, &nb_name); + nb_u8arr_create(&nb_desc, strlen(kv->desc), kv->desc); + nb_add_u8arr(nb, &nb_desc); + + nb_print(nb); + nb_print(nb); + + return nb; } -void kv_free() +void kv_free(kv_user_name *kv) { - + if (kv!=NULL) + { + free(kv->name); + free(kv->desc); + free(kv); + } } @@ -34,19 +100,26 @@ int main() FILE *f; int i=0; - netbyte_store nb; + kv_user_name *kv=NULL; + + netbyte_store *nb; printf("Start test\n"); - nb_init( &nb ); - f = fopen("test_many.nb","r"); - while (nb_fread( &nb, fileno(f) ) == 0) - { - printf("ITER %d: ", i ); - pr_store( &nb ); - i += 1; - } - fclose( f ); + kv = kv_new(1,"Name","Name of the human"); + nb = kv_marsh(kv); + nb_print(nb); + kv_free(kv); + + //nb_print(nb); + res = nb_create(nb); + + f = fopen("test_kv_save.nd","w+"); + fwrite(res, 1, nb->size, f); + fclose(f); + + free(res); + nb_free(nb); printf("End test\n"); return 0; diff --git a/test/test_multiple_write.c b/test/test_multiple_write.c index 53d7cae..9e23185 100644 --- a/test/test_multiple_write.c +++ b/test/test_multiple_write.c @@ -64,10 +64,10 @@ int main() uint8_t *res; FILE *f; - netbyte_store nb; + netbyte_store *nb=malloc(sizeof(netbyte_store)); nb_u8arr u8arr; - nb_init( &nb ); + nb_init( nb ); er = nb_u8arr_create( &u8arr, len, &str[0] ); if (er) @@ -75,22 +75,25 @@ int main() pr_u8arr( &u8arr ); - er = nb_add_u8arr( &nb, &u8arr ); + er = nb_add_u8arr( nb, &u8arr ); if (er) printf("er add u8arr: %d\n",er); - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); f = fopen("test_many.nb","w+"); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); + free(res); + nb_free(nb); + printf("End test\n"); return 0; } \ No newline at end of file diff --git a/test/test_save.c b/test/test_save.c index 6e3be63..6c1f0ae 100644 --- a/test/test_save.c +++ b/test/test_save.c @@ -59,8 +59,8 @@ int main() { FILE *f=NULL; int er; - netbyte_store nb; - netbyte_store nb2; + netbyte_store *nb=malloc(sizeof(netbyte_store)); + netbyte_store *nb2=malloc(sizeof(netbyte_store)); nb_u8 u8; nb_u8arr u8arr; const int len = 11; @@ -69,7 +69,7 @@ int main() printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); er = nb_u8_create( &u8, 0x10 ); if (er) printf("er create u8: %d\n",er); @@ -82,45 +82,51 @@ int main() pr_u8arr( &u8arr ); #if 1 - pr_store( &nb ); - er = nb_add_u8( &nb, &u8 ); + pr_store( nb ); + er = nb_add_u8( nb, &u8 ); if (er) printf("er add u8: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u8arr( &nb, &u8arr ); + pr_store( nb ); + er = nb_add_u8arr( nb, &u8arr ); if (er) printf("er add u8arr: %d\n",er); #endif - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); printf("res 0x%x\n", res); f = fopen("test.nb","w+"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); printf("LOAD:-----\n"); - nb_init( &nb2 ); - pr_store( &nb2 ); - nb_load( &nb2, res ); - pr_store( &nb2 ); + nb_init( nb2 ); + pr_store( nb2 ); + - printf( "->1 [%s]\n", ((nb_u8arr *)nb2.types[1].nb_val)->val ); + nb_load( nb2, res ); + pr_store( nb2 ); + + + printf( "->1 [%s]\n", ((nb_u8arr *)nb2->types[1].nb_val)->val ); __NBT_TYPED *t=0xf8; nb_u8arr *v; - nb_type( &nb2, 1, &t ); + nb_type( nb2, 1, &t ); printf("get type: %02x\n", t ); - nb_val( &nb2, 1, (uint8_t **)&v ); + nb_val( nb2, 1, (uint8_t **)&v ); printf("get value: %02x\n", v->val ); - free( res ); + + nb_free(nb); + nb_free(nb2); + free( res ); res = NULL; printf("End test\n"); return 0; -- cgit v1.2.3