修复多处内存泄漏。

This commit is contained in:
zhengchao
2018-11-29 16:24:45 +08:00
parent bcfed2bab1
commit f76a8c5ca2
7 changed files with 48 additions and 28 deletions

View File

@@ -291,8 +291,8 @@ typedef void (http_session_end_cb)(const struct tfe_stream * stream,
/* Tools functions for standard HTTP header field name */ /* Tools functions for standard HTTP header field name */
struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig); struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig);
int http_field_name_compare(const struct http_field_name * lvalue, const struct http_field_name * rvalue); int http_field_name_compare(const struct http_field_name * lvalue, const struct http_field_name * rvalue);
const char * http_field_to_string(const struct http_field_name * field); const char * http_field_name_to_string(const struct http_field_name * field);
void http_field_destory(struct http_field_name *); void http_field_name_destory(struct http_field_name *);
/* Tools functions for standard HTTP method */ /* Tools functions for standard HTTP method */
const char * http_std_method_to_string(enum tfe_http_std_method method); const char * http_std_method_to_string(enum tfe_http_std_method method);

View File

@@ -117,6 +117,7 @@ const char * http_field_to_string(const struct http_field_name * field)
void http_field_destory(struct http_field_name * field) void http_field_destory(struct http_field_name * field)
{ {
if(field->field_id == TFE_HTTP_UNKNOWN_FIELD) FREE(&field->field_name);
free(field); free(field);
} }

View File

@@ -20,6 +20,7 @@ struct tfe_rpc_ctx
{ {
struct event_base * evbase; struct event_base * evbase;
enum TFE_RPC_FLAG flag; enum TFE_RPC_FLAG flag;
struct evhttp_connection* connection;
}; };
/* /*
@@ -41,6 +42,9 @@ int read_header_done_cb(struct evhttp_request* response, void* arg)
static void tfe_rpc_promise_free_ctx(void* ctx) static void tfe_rpc_promise_free_ctx(void* ctx)
{ {
struct tfe_rpc_ctx* _ctx=(struct tfe_rpc_ctx*)ctx;
evhttp_connection_free(_ctx->connection);
_ctx->connection=NULL;
free(ctx); free(ctx);
ctx = NULL; ctx = NULL;
return; return;
@@ -190,13 +194,13 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me
if(NULL == uri) if(NULL == uri)
{ {
_wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse url failed!"); _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse url failed!");
return; goto error_out;
} }
const char* host = evhttp_uri_get_host(uri); const char* host = evhttp_uri_get_host(uri);
if(!host) if(!host)
{ {
_wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse host failed!"); _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "parse host failed!");
return; goto error_out;
} }
int port = evhttp_uri_get_port(uri); int port = evhttp_uri_get_port(uri);
if(port < 0) if(port < 0)
@@ -204,13 +208,13 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me
port = 80; port = 80;
} }
struct evhttp_connection* connection = evhttp_connection_base_new(evbase, dnsbase, host, port); ctx->connection = evhttp_connection_base_new(evbase, dnsbase, host, port);
if (!connection) if (!ctx->connection)
{ {
_wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "create connection failed!"); _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "create connection failed!");
return; goto error_out;
} }
evhttp_connection_set_closecb(connection, connection_close_cb, evbase); evhttp_connection_set_closecb(ctx->connection, connection_close_cb, evbase);
struct evhttp_request* request = evhttp_request_new(get_response_cb, (void*)p); struct evhttp_request* request = evhttp_request_new(get_response_cb, (void*)p);
//evhttp_request_set_header_cb(request, read_header_done_cb); //evhttp_request_set_header_cb(request, read_header_done_cb);
if(flag == CHUNK_CB) if(flag == CHUNK_CB)
@@ -224,23 +228,31 @@ void tfe_rpc_async_ask(struct future* f, const char* url, enum TFE_RPC_METHOD me
//printf("request url is %s\n", request_url); //printf("request url is %s\n", request_url);
if(request_url == NULL) if(request_url == NULL)
{ {
_wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "get request url failed"); _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "get request url failed");
return; goto error_out;
} }
switch(method) switch(method)
{ {
case GET: case GET:
evhttp_make_request(connection, request, EVHTTP_REQ_GET, request_url); evhttp_make_request(ctx->connection, request, EVHTTP_REQ_GET, request_url);
break; break;
case POST: case POST:
evbuffer_add(request->output_buffer, data, data_len); evbuffer_add(request->output_buffer, data, data_len);
evhttp_make_request(connection, request, EVHTTP_REQ_POST, request_url); evhttp_make_request(ctx->connection, request, EVHTTP_REQ_POST, request_url);
break; break;
default: default:
_wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "method is invalid!"); _wrapped_promise_failed(p, FUTURE_ERROR_EXCEPTION, "method is invalid!");
return; goto error_out;
} }
free(request_url); free(request_url);
evhttp_uri_free(uri);
return;
error_out:
if(uri) evhttp_uri_free(uri);
promise_dettach_ctx(p);
tfe_rpc_promise_free_ctx(ctx);
return;
} }
struct tfe_rpc_response_result* tfe_rpc_release(void* result) struct tfe_rpc_response_result* tfe_rpc_release(void* result)

View File

@@ -300,12 +300,12 @@ static struct keyring_private* get_keyring_from_response(const char* data)
STACK_OF(X509)* chain = NULL; STACK_OF(X509)* chain = NULL;
if(data == NULL) if(data == NULL)
{ {
return NULL; goto error_out;
} }
cJSON* data_json = cJSON_Parse(data); cJSON* data_json = cJSON_Parse(data);
if(data_json == NULL) if(data_json == NULL)
{ {
return NULL; goto error_out;
} }
cJSON* cert_json = NULL; cJSON* cert_json = NULL;
cJSON* key_json = NULL; cJSON* key_json = NULL;
@@ -319,8 +319,7 @@ static struct keyring_private* get_keyring_from_response(const char* data)
} }
if(cert == NULL) if(cert == NULL)
{ {
err_out(cert, key, chain); goto error_out;
return NULL;
} }
if (key_json && key_json->valuestring != NULL) if (key_json && key_json->valuestring != NULL)
{ {
@@ -328,13 +327,11 @@ static struct keyring_private* get_keyring_from_response(const char* data)
} }
if(key == NULL) if(key == NULL)
{ {
err_out(cert, key, chain); goto error_out;
return NULL;
} }
if(chain_json == NULL) if(chain_json == NULL)
{ {
err_out(cert, key, chain); goto error_out;
return NULL;
} }
cJSON* chain_cert_json = NULL; cJSON* chain_cert_json = NULL;
chain = sk_X509_new_null(); chain = sk_X509_new_null();
@@ -347,8 +344,7 @@ static struct keyring_private* get_keyring_from_response(const char* data)
} }
if(chain_cert == NULL) if(chain_cert == NULL)
{ {
err_out(cert, key, chain); goto error_out;
return NULL;
} }
sk_X509_push(chain, chain_cert); sk_X509_push(chain, chain_cert);
ssl_x509_refcount_inc(chain_cert); ssl_x509_refcount_inc(chain_cert);
@@ -359,8 +355,17 @@ static struct keyring_private* get_keyring_from_response(const char* data)
keyring_set_chain(_kyr, chain); keyring_set_chain(_kyr, chain);
X509_free(cert); X509_free(cert);
EVP_PKEY_free(key); EVP_PKEY_free(key);
sk_X509_pop_free(chain, X509_free); sk_X509_pop_free(chain, X509_free);
cJSON_free(data_json);
return _kyr; return _kyr;
error_out:
if(data_json!=NULL) cJSON_free(data_json);
if(cert) X509_free(cert);
if(key) EVP_PKEY_free(key);
if(chain) sk_X509_pop_free(chain, X509_free);
return NULL;
} }
static long keyring_local_cache_query_cb(void * data, const uchar * key, uint size, void * user_arg) static long keyring_local_cache_query_cb(void * data, const uchar * key, uint size, void * user_arg)
@@ -600,6 +605,7 @@ char* url_escape(char* url)
{ {
_url = curl_easy_escape(curl, url, strlen(url)); _url = curl_easy_escape(curl, url, strlen(url));
} }
curl_easy_cleanup(curl);
return _url; return _url;
} }

View File

@@ -1434,7 +1434,7 @@ void ssl_connect_client_ctx_free(struct ssl_connect_client_ctx * ctx)
{ {
ssl_stream_free(ctx->downstream); ssl_stream_free(ctx->downstream);
} }
free(ctx);
return; return;
} }

View File

@@ -463,6 +463,7 @@ void http_repl_ctx_free(struct replace_ctx* rep_ctx)
FREE(&(rep_ctx->rule[i].find)); FREE(&(rep_ctx->rule[i].find));
FREE(&(rep_ctx->rule[i].replace_with)); FREE(&(rep_ctx->rule[i].replace_with));
} }
FREE(&(rep_ctx->rule));
if (rep_ctx->http_body) if (rep_ctx->http_body)
{ {
evbuffer_free(rep_ctx->http_body); evbuffer_free(rep_ctx->http_body);
@@ -939,7 +940,7 @@ enum pangu_action http_scan(const struct tfe_http_session * session, enum tfe_ht
break; break;
} }
const char * str_field_name = http_field_to_string(&field_name); const char * str_field_name = http_field_name_to_string(&field_name);
scan_ret = Maat_set_scan_status(g_pangu_rt->maat, &(ctx->scan_mid), MAAT_SET_SCAN_DISTRICT, scan_ret = Maat_set_scan_status(g_pangu_rt->maat, &(ctx->scan_mid), MAAT_SET_SCAN_DISTRICT,
str_field_name, strlen(str_field_name)); str_field_name, strlen(str_field_name));

View File

@@ -709,7 +709,7 @@ void hf_private_destory(struct http_half_private * hf_private)
TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer) TAILQ_FOREACH_SAFE(header_iter, &hf_private->header_list, next, header_titer)
{ {
TAILQ_REMOVE(&hf_private->header_list, header_iter, next); TAILQ_REMOVE(&hf_private->header_list, header_iter, next);
free(header_iter->field); http_field_name_destory(header_iter->field);
free(header_iter->value); free(header_iter->value);
free(header_iter); free(header_iter);
} }
@@ -1110,7 +1110,7 @@ void hf_private_construct(struct http_half_private * hf_private)
for (const char * str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name); for (const char * str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name);
str_value != NULL; str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name)) str_value != NULL; str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name))
{ {
const char * str_field = http_field_to_string(&field_name); const char * str_field = http_field_name_to_string(&field_name);
evbuffer_add_printf(hf_private->evbuf_raw, "%s: %s\r\n", str_field, str_value); evbuffer_add_printf(hf_private->evbuf_raw, "%s: %s\r\n", str_field, str_value);
} }