From 4679f6ab04ce4a06180e6d18141c967d606b5fd6 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Wed, 14 Mar 2018 11:55:15 +0100 Subject: [PATCH] fix memory leak bug --- http_server.c | 11 +++++++++-- libs/utils.c | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/http_server.c b/http_server.c index 5b77625..78d46e7 100644 --- a/http_server.c +++ b/http_server.c @@ -19,8 +19,13 @@ void accept_request(void* client) //char *query_string = NULL; //LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock); numchars = read_buf(client, buf, sizeof(buf)); + if(numchars < 0) + { + unknow(client); + goto end; + } i = 0; j = 0; - while (!ISspace(buf[j]) && (i < sizeof(method) - 1)) + while (j < numchars && !ISspace(buf[j]) && (i < sizeof(method) - 1)) { method[i] = buf[j]; i++; j++; @@ -108,7 +113,9 @@ void accept_request(void* client) char* mime_type = mime(path); if(strcmp(mime_type,"application/octet-stream") == 0) { - char* h = dvalue(server_config.handlers,ext(path)); + char * ex = ext(path); + char* h = dvalue(server_config.handlers,ex); + if(ex) free(ex); if(h) { sprintf(buf,"/%s%s",h,url); diff --git a/libs/utils.c b/libs/utils.c index c0a367b..5a5d967 100644 --- a/libs/utils.c +++ b/libs/utils.c @@ -81,11 +81,11 @@ char* __s(const char* fstring,...) */ void trim(char* str, const char delim) { - if(!str) return; + if(!str || strlen(str) == 0) return; char * p = str; int l = strlen(p); - - while(p[l - 1] == delim) p[--l] = 0; + while(l > 0 && p[l - 1] == delim) + p[--l] = 0; while(* p && (* p) == delim ) ++p, --l; memmove(str, p, l + 1); }