fix memory leak

This commit is contained in:
Xuan Sang LE
2018-03-02 19:04:00 +01:00
parent 3029555f59
commit 6bc76386f6
13 changed files with 231 additions and 58 deletions

View File

@ -81,7 +81,10 @@ void add_record(dbrecord* r,dbfield f)
new_r->idx = 1;
new_r->next = NULL;
if((*r)->idx == 0)
{
freerecord(&(*r));
*r = new_r;
}
else
{
dbrecord* temp;
@ -157,8 +160,8 @@ dbrecord dbselect(sqlite3* db, const char* table,const char* fstring,...)
dbfield fields = __field();
for(int col = 0; col < cols; col++)
{
char *value = (char*)sqlite3_column_text(statement, col);
char *name = (char*)sqlite3_column_name(statement, col);
const char *value = sqlite3_column_text(statement, col);
const char *name = sqlite3_column_name(statement, col);
add_field(&fields,name,(value!=0)?value:"");
}
add_record(&records,fields);
@ -180,8 +183,12 @@ int hastable(sqlite3* db,const char* table)
dbrecord rc = dbselect(db,"sqlite_master","type='table' and name='%s'", table);
//free(prefix);
if(!rc) return 0;
if(!rc->fields) return 0;
free(rc);
if(!rc->fields)
{
freerecord(&rc);
return 0;
}
freerecord(&rc);
return 1;
}
int dbupdate(sqlite3* db,const char* table,const dbfield field,const char* fstring,...)
@ -228,4 +235,26 @@ int dbdelete(sqlite3* db,const char* table,const char* fstring,...)
free(cond);
free(sql);
return ret;
}
}
void freerecord(dbrecord * r)
{
dbrecord curr;
while ((curr = (*r)) != NULL) {
(*r) = (*r)->next;
if(curr->fields)
freefield(&(curr->fields));
free (curr);
}
}
void freefield(dbfield *f)
{
dbfield curr;
while( (curr = (*f)) != NULL )
{
(*f) = (*f)->next;
if(curr->name) free(curr->name);
if(curr->value) free(curr->value);
free(curr);
}
}

View File

@ -34,4 +34,6 @@ char* value_of(const dbfield,const char*);
void add_field(dbfield*,const char*, const char*);
void add_record(dbrecord*,dbfield);
void dbclose(sqlite3*);
void freerecord(dbrecord *);
void freefield(dbfield *);
#endif

View File

@ -93,5 +93,29 @@ void* dvalue(dictionary dic, const char* key)
return as->value;
}
void freedict(dictionary dic){free(dic);}
void free_association(association * asoc)
{
while( (*asoc) != NULL )
{
association a = *asoc;
(* asoc) = (*asoc) ->next;
if(a->key)
{
free(a->key);
if(a->value) free(a->value);
}
free(a);
}
}
void freedict(dictionary dic){
for(int i = 0; i < HASHSIZE; i++)
free_association(&(dic[i]));
free(dic);
}

View File

@ -9,8 +9,12 @@ int usessl()
void set_status(void* client,int code,const char* msg)
{
response(client, __s("HTTP/1.1 %d %s", code, msg));
response(client, __s("Server: %s ", SERVER_NAME));
char *s = __s("HTTP/1.1 %d %s", code, msg);
response(client, s);
free(s);
s = __s("Server: %s ", SERVER_NAME);
response(client, s);
free(s);
}
void redirect(void* client,const char*path)
{
@ -67,7 +71,7 @@ int response(void* client, const char* data)
}
int antd_send(const void *src, const void* data, int len)
{
if(!src) return -1;
if(!src || !data) return -1;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(usessl())
@ -146,7 +150,11 @@ int __t(void* client, const char* fstring,...)
va_end(arguments);
if(dlen < BUFFLEN)
return response(client,data);
{
int ret = response(client,data);
free(data);
return ret;
}
else
{
while(sent < dlen - 1)
@ -163,7 +171,12 @@ int __t(void* client, const char* fstring,...)
sent += buflen;
nbytes = antd_send(client, chunk, buflen);
free(chunk);
if(nbytes == -1) return 0;
if(nbytes == -1)
{
//free(data);
//return 0;
break;
}
}
chunk = "\r\n";
antd_send(client, chunk, strlen(chunk));

View File

@ -34,6 +34,7 @@ void list_put(list* l, item it)
{
if(*l == NULL || (*l)->type == RPC_TYPE_NIL)
{
free(*l);
*l = it;
return ;
}
@ -167,6 +168,7 @@ list split(const char* str, const char* delim)
{
if(str == NULL || delim == NULL) return NULL;
char* str_cpy = strdup(str);
char* org_str = str_cpy;
char* token;
list l = list_init();
while((token = strsep(&str_cpy,delim)))
@ -176,7 +178,7 @@ list split(const char* str, const char* delim)
list_put_special(&l,token);
}
}
free(str_cpy);
free(org_str);
if(l->type== RPC_TYPE_NIL)
return NULL;
return l;
@ -227,9 +229,15 @@ void list_free(list *l)
{
item curr;
while ((curr = (*l)) != NULL) {
(*l) = (*l)->next;
(*l) = (*l)->next;
if(curr->type == RPC_TYPE_ARRAY)
list_free(&curr->value.array);
else if(curr->type == RPC_TYPE_STRING)
free(curr->value.s);
else if(curr->type == RPC_TYPE_DATE)
free(curr->value.date);
else if(curr->type == RPC_TYPE_BASE64)
free(curr->value.b64);
free (curr);
}
}

View File

@ -73,4 +73,13 @@ char* config_dir()
if (stat(path, &st) == -1)
mkdir(path, 0755);
return path;
}
void __release()
{
printf("Releasing plugin\n");
if(__plugin__.name) free(__plugin__.name);
if(__plugin__.dbpath) free(__plugin__.dbpath);
if(__plugin__.htdocs) free(__plugin__.htdocs);
if(__plugin__.pdir) free(__plugin__.pdir);
}

View File

@ -37,5 +37,5 @@ char* htdocs(const char*);
char* config_dir();
/*Default function for plugin*/
void handler(void*, const char*,const char*,dictionary);
void __release();
#endif

View File

@ -143,13 +143,15 @@ char* ext(const char* file)
char* token,*ltoken = "";
if(file == NULL) return NULL;
char* str_cpy = strdup(file);
char* str_org = str_cpy;
if(strstr(str_cpy,".")<= 0) return "";
if(*file == '.')
trim(str_cpy,'.');
while((token = strsep(&str_cpy,".")) && strlen(token)>0) {ltoken = token;}
free(str_cpy);
return ltoken;
char* ext = strdup(ltoken);
free(str_org);
return ext;
}
/*get mime file info from extension*/
@ -182,6 +184,7 @@ char* mime(const char* file)
{
char * ex = ext(file);
mime_t m = mime_from_ext(ex);
free(ex);
return m.type;
}
@ -189,6 +192,7 @@ int is_bin(const char* file)
{
char * ex = ext(file);
mime_t m = mime_from_ext(ex);
free(ex);
return m.bin;
}

View File

@ -15,7 +15,7 @@ static void ws_gen_mask_key(ws_msg_header_t * header)
ws_msg_header_t * ws_read_header(void* client)
{
uint8_t byte;
uint8_t byte = 0;
uint8_t bytes[8];
ws_msg_header_t* header = (ws_msg_header_t*) malloc(sizeof(*header));

View File

@ -89,12 +89,14 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
{
LOG("%s\n","Data is not mask");
write(fdm, "exit\n", 5);
free(h);
return;
}
if(h->opcode == WS_CLOSE)
{
LOG("%s\n","Websocket: connection closed");
write(fdm, "exit\n", 5);
free(h);
return;
}
else if(h->opcode == WS_TEXT)
@ -102,6 +104,7 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
int l;
char * tok = NULL;
char* tok1 = NULL;
char* orgs = NULL;
while((l = ws_read_data(cl,h,sizeof(buff),buff)) > 0)
{
char c = buff[0];
@ -114,6 +117,7 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
case 's': // terminal resize event
buff[l] = '\0';
tok = strdup(buff+1);
orgs = tok;
tok1 = strsep(&tok,":");
if(tok != NULL && tok1 != NULL)
{
@ -136,6 +140,8 @@ void handler(void* cl, const char* m, const char* rqp, dictionary rq)
if (ioctl(fdm, TIOCSWINSZ, (char *) &win) != 0)
printf("Cannot set winsize\n");
free(orgs);
}
break;