debug leak mem in fmock

This commit is contained in:
2024-02-09 13:16:31 +01:00
parent e1171d720b
commit 9c58ca2873
5 changed files with 123 additions and 15 deletions
+23 -5
View File
@@ -52,6 +52,10 @@ void append_fmock_to_listmock(struct func_mock_info_struct **f_mock_list, struc
void append_list_base_fmock(struct list_base_fmock **l_fmock, struct func_mock_info_struct *f_mock);
void append_variable_current(struct list_current_variable **lcurrent_var, char *current_var);
void clear_fmock_info_list(struct func_mock_info_struct **f_mock_list);
void clear_list_base_fmock(struct list_base_fmock **l_fmock);
void clear_variable_current(struct list_current_variable **lcurrent_var);
// if input == functioname___line_NBLINE return functioname, else it return the input
//char* extract_name_func_mock(char *input);
@@ -72,7 +76,7 @@ extern struct list_base_fmock *g_list_base_fmock;
((tmp__mock)->info_mock)->expect_call = -1;\
((tmp__mock)->info_mock)->call = 0;\
((tmp__mock)->info_mock)->failed_call = 0;\
((tmp__mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 32 + strlen(#pre_id));\
((tmp__mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 43 + strlen(#pre_id));\
sprintf(((tmp__mock)->info_mock)->str_namefunc,"%s%s%d",#namefunction,#pre_id,id);\
((tmp__mock)->info_mock)->str_conditions = NULL;\
((tmp__mock)->info_mock)->str_caller = NULL;\
@@ -102,6 +106,19 @@ extern struct list_base_fmock *g_list_base_fmock;
list_mo_ ## namefunction.str_print_current_variables = NULL;\
list_mo_ ## namefunction.next = NULL;\
}\
\
__attribute__((destructor)) void destruct_list_m_ ## namefunction(void){ \
/*free(list_mo_ ## namefunction.info_mock);*/ \
struct list_mock_return_ ## namefunction *tmp_nn = list_mo_ ## namefunction.next, *ttmp_nn;\
while(tmp_nn){\
ttmp_nn = tmp_nn;\
tmp_nn = tmp_nn->next;\
/*free(ttmp_nn->info_mock);*/\
free(ttmp_nn);\
}\
\
}\
\
returntype namefunction args_prototype_with_parenthesis {\
static size_t count_call_f=0;\
++count_call_f;\
@@ -157,7 +174,7 @@ extern struct list_base_fmock *g_list_base_fmock;
else if (((tmp_mock->info_mock)->times_left != 0) && ((tmp_mock->info_mock)->times_left != INITSTATE )) {\
size_t len0 = strlen((tmp_mock->info_mock)->str_conditions);\
size_t len1 = strlen("when checking condition call: aa");\
char *msg_call=malloc(len0 + len1 + strlen(__func__));\
char *msg_call=malloc(len0 + len1 + strlen(__func__)+1);\
sprintf(msg_call,"when checking %s condition call: %s",__func__,(tmp_mock->info_mock)->str_conditions);\
HANDLE_OP_EXPECT_NAME(EQ,TYPE_INT,1, tmp_mock->call_mock_condition args_call_with_parenthesis, (tmp_mock->info_mock)->str_caller, msg_call); /*LOG("%s\n","failure condition");*/\
free(msg_call);\
@@ -201,7 +218,7 @@ char* extract_name_func_mock(char *input);
do{\
struct list_mock_return_ ## namefunction *tmp_mock = &list_mo_ ## namefunction;\
while(tmp_mock){\
(tmp_mock->info_mock)->str_caller=malloc(strlen(__func__));\
(tmp_mock->info_mock)->str_caller=malloc(strlen(__func__)+1);\
strcpy((tmp_mock->info_mock)->str_caller,__func__);\
tmp_mock = tmp_mock->next;\
}\
@@ -235,9 +252,9 @@ __attribute__((constructor)) void create_str_print_variables ## namefunction(){\
((tmp_new_mock)->info_mock)->failed_call = 0;\
((tmp_new_mock)->info_mock)->init_times_left = repeat;\
((tmp_new_mock)->info_mock)->times_left = repeat;\
((tmp_new_mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 32 + strlen(#pre_id));\
((tmp_new_mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 43 + strlen(#pre_id));\
sprintf(((tmp_new_mock)->info_mock)->str_namefunc,"%s%s%d",#namefunction,#pre_id,id);\
((tmp_new_mock)->info_mock)->str_conditions = malloc(strlen(#condition_on_args_expression));\
((tmp_new_mock)->info_mock)->str_conditions = malloc(strlen(#condition_on_args_expression)+1);\
strcpy(((tmp_new_mock)->info_mock)->str_conditions, #condition_on_args_expression);\
((tmp_new_mock)->info_mock)->str_caller = NULL;\
((tmp_new_mock)->info_mock)->l_current_var= NULL;\
@@ -251,6 +268,7 @@ __attribute__((constructor)) void create_str_print_variables ## namefunction(){\
returntype CONCAT(run_ ## namefunction ## pre_id , id) args_prototype_with_parenthesis; \
int CONCAT(namefunction ## _cond_ , id) args_prototype_with_parenthesis {/*LOG("cond:%d\n",condition_on_args_expression);*/ return condition_on_args_expression;}\
__attribute__((constructor)) void CONCAT(append_list_ ## namefunction , id)(void){\
PRINT_DEBUG(" in constructor fmock : func :%s\n",#namefunction);\
struct list_mock_return_ ## namefunction *tmp_mock = &list_mo_ ## namefunction;\
if((tmp_mock->info_mock)->times_left == INITSTATE){/* init state */\
FILL_MOCK_INFO(tmp_mock, namefunction, condition_on_args_expression , repeat, f_expect_call, pre_id, id, true);\
+63 -4
View File
@@ -60,6 +60,7 @@ void append_list_base_fmock(struct list_base_fmock **l_fmock, struct func_mock_i
else{
*l_fmock = malloc(sizeof(struct list_base_fmock));
(*l_fmock)->info_mock = f_mock;
(*l_fmock)->next = NULL;
}
UNLOCK(mut_g_list_base_fmock);
}
@@ -113,7 +114,7 @@ void append_fmock_to_listmock(struct func_mock_info_struct **f_mock_list, struc
#define SSTRFY(x) STRFY(x)
char* extract_name_func_mock(char *input){
char * ret=malloc(strlen(input));
char * ret=malloc(strlen(input) + 1);
strcpy(ret,input);
char * pre_id_ = SSTRFY(PRE_ID);
size_t len_pre_id_ = strlen(pre_id_);
@@ -156,7 +157,7 @@ char * number_call_translate(long nb){
char * strprint_caller_(char *input){
if(input==NULL) { return "";}
char *called_in=",\t called in ";
char *ret = malloc(strlen(called_in)+strlen(input)+1);
char *ret = malloc(strlen(called_in) + strlen(input) + 1);
sprintf(ret,"%s%s",called_in,input);
return ret;
}
@@ -192,7 +193,7 @@ __attribute__((destructor))
struct winsize w;
ioctl(1, TIOCGWINSZ, &w);
char *reader=malloc(w.ws_col);
char *reader=malloc(w.ws_col+1);
strcpy(reader,"STAT OF MOCK FUNCTIONS");
fprintf(F_OUT,"%s\n\n%0*d\n %*s \n%0*d %s\n\n", colors_f[k_YELLOW] ,w.ws_col,0, (int)(w.ws_col+strlen(reader))/2, reader,w.ws_col,0, DEFAULT_K );
@@ -237,7 +238,9 @@ __attribute__((destructor))
memset(reader,'=',w.ws_col);
reader[w.ws_col-1]='\0';
char *caller="";
if(tmp_inf_mock->str_caller) caller = extract_func_edited_TEST_from_exec_func_name(tmp_inf_mock->str_caller);
if(tmp_inf_mock->str_caller){
caller = extract_func_edited_TEST_from_exec_func_name(tmp_inf_mock->str_caller);
}
size_t len_caller = strlen(caller);
char *nameff=extract_name_func_mock(tmp_inf_mock->str_namefunc);
@@ -319,3 +322,59 @@ __attribute__((destructor))
PRINT_DEBUG("%s\n","pthread_mutex_destroy done!");
PRINT_DEBUG("%s\n","check mock done!");
}
void clear_fmock_info_list(struct func_mock_info_struct **f_mock_list){
if(*f_mock_list){
struct func_mock_info_struct *tmp_fmock_info = *f_mock_list, *ttmp_fmock_info;
while(tmp_fmock_info){
ttmp_fmock_info = tmp_fmock_info;
tmp_fmock_info = tmp_fmock_info->next;
free(ttmp_fmock_info->str_namefunc);
free(ttmp_fmock_info->str_conditions);
free(ttmp_fmock_info->str_caller);
clear_variable_current(&(ttmp_fmock_info->l_current_var));
free(ttmp_fmock_info);
}
*f_mock_list = NULL;
}
}
void clear_list_base_fmock(struct list_base_fmock **l_fmock){
if(*l_fmock){
struct list_base_fmock *tmp_l_n = *l_fmock, *ttmp_l_n;
while(tmp_l_n){
ttmp_l_n = tmp_l_n;
tmp_l_n = tmp_l_n->next;
//clear_fmock_info_list(&((ttmp_l_n->next)->info_mock));
free(ttmp_l_n);
}
*l_fmock=NULL;
}
}
void clear_variable_current(struct list_current_variable **lcurrent_var){
if(*lcurrent_var){
struct list_current_variable *tmp_lcv = *lcurrent_var, *ttmp_lcv;
while(tmp_lcv){
ttmp_lcv = tmp_lcv;
tmp_lcv = tmp_lcv->next;
free(ttmp_lcv->str_current_variables);
free(ttmp_lcv);
}
*lcurrent_var=NULL;
}
}
__attribute__((constructor))
void purge_fmock(){
clear_fmock_info_list(&f_mock_glist);
PRINT_DEBUG("purge f_mock_glist %s\n","done");
//clear_list_base_fmock(&g_list_base_fmock);
//PRINT_DEBUG("purge g_list_base_fmock %s\n","done");
}