fix memory leaks (#191)

This commit is contained in:
kirill reshke 2020-07-09 10:19:34 +05:00 committed by GitHub
parent 79fe224f2f
commit a1be256a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 94 additions and 56 deletions

View File

@ -767,13 +767,18 @@ od_config_reader_route(od_config_reader_t *reader,
/* quantiles */
case OD_LQUANTILES: {
char *quantiles_str = NULL;
if (!od_config_reader_string(reader, &quantiles_str))
return -1;
if (!od_config_reader_string(reader, &quantiles_str)) {
free(quantiles_str);
return NOT_OK_RESPONSE;
}
if (!od_config_reader_quantiles(reader,
quantiles_str,
&route->quantiles,
&route->quantiles_count))
return -1;
&route->quantiles_count)) {
free(quantiles_str);
return NOT_OK_RESPONSE;
}
free(quantiles_str);
} break;
/* application_name_add_host */
case OD_LAPPLICATION_NAME_ADD_HOST:

View File

@ -4,25 +4,29 @@
inline od_counter_llist_t *
od_counter_llist_create(void)
{
od_counter_llist_t *l = malloc(sizeof(od_counter_llist_t));
od_counter_llist_t *llist = malloc(sizeof(od_counter_llist_t));
if (llist == NULL)
return NULL;
l->list = NULL;
l->count = 0;
llist->list = NULL;
llist->count = 0;
return l;
return llist;
}
inline void
od_counter_llist_add(od_counter_llist_t *l, const od_counter_item_t *it)
od_counter_llist_add(od_counter_llist_t *llist, const od_counter_item_t *it)
{
od_counter_litem_t *litem = malloc(sizeof(od_counter_litem_t));
litem->value = *it;
litem->cnt = 1;
if (litem == NULL)
return;
litem->value = *it;
litem->cnt = 1;
litem->next = l->list;
l->list = litem;
litem->next = llist->list;
llist->list = litem;
++l->count;
++llist->count;
}
inline od_retcode_t
@ -42,13 +46,27 @@ od_counter_t *
od_counter_create(size_t sz)
{
od_counter_t *t = malloc(sizeof(od_counter_t));
t->buckets = malloc(sizeof(od_counter_llist_t) * sz);
if (t == NULL) {
return NULL;
}
t->buckets = malloc(sizeof(od_counter_llist_t) * sz);
if (t->buckets == NULL) {
return NULL;
}
t->bucket_mutex = malloc(sizeof(pthread_mutex_t) * sz);
t->size = sz;
if (t->bucket_mutex == NULL) {
return NULL;
}
t->size = sz;
for (size_t i = 0; i < t->size; ++i) {
t->buckets[i] = od_counter_llist_create();
pthread_mutex_init(&t->bucket_mutex[i], NULL);
if (t->buckets[i] == NULL)
return NULL;
const int res = pthread_mutex_init(&t->bucket_mutex[i], NULL);
if (res) {
return NULL;
}
}
return t;

View File

@ -8,7 +8,6 @@
* Scalable PostgreSQL connection pooler.
*/
#include <malloc.h>
#include <kiwi.h>
#include "macro.h"
@ -40,7 +39,7 @@ extern od_counter_llist_t *
od_counter_llist_create(void);
extern void
od_counter_llist_add(od_counter_llist_t *l, const od_counter_item_t *it);
od_counter_llist_add(od_counter_llist_t *llist, const od_counter_item_t *it);
extern od_retcode_t
od_counter_llist_free(od_counter_llist_t *l);

View File

@ -4,33 +4,39 @@
od_error_logger_t *
od_err_logger_create(size_t intervals_count)
{
od_error_logger_t *l = malloc(sizeof(od_error_logger_t));
l->intercals_cnt = intervals_count;
l->current_interval_num = 0;
l->interval_counters = malloc(sizeof(od_counter_t) * intervals_count);
for (size_t i = 0; i < intervals_count; ++i) {
l->interval_counters[i] = od_counter_create_default();
od_error_logger_t *err_logger = malloc(sizeof(od_error_logger_t));
if (err_logger == NULL) {
return NULL;
}
pthread_mutex_init(&l->lock, NULL);
err_logger->intercals_cnt = intervals_count;
err_logger->current_interval_num = 0;
return l;
err_logger->interval_counters =
malloc(sizeof(od_counter_t) * intervals_count);
for (size_t i = 0; i < intervals_count; ++i) {
err_logger->interval_counters[i] = od_counter_create_default();
if (err_logger->interval_counters[i] == NULL)
return NULL;
}
pthread_mutex_init(&err_logger->lock, NULL);
return err_logger;
}
od_retcode_t
od_err_logger_free(od_error_logger_t *l)
od_err_logger_free(od_error_logger_t *err_logger)
{
for (size_t i = 0; i < l->intercals_cnt; ++i) {
int rc = od_counter_free(l->interval_counters[i]);
for (size_t i = 0; i < err_logger->intercals_cnt; ++i) {
int rc = od_counter_free(err_logger->interval_counters[i]);
if (rc != OK_RESPONSE)
return rc;
}
pthread_mutex_destroy(&l->lock);
free(l);
pthread_mutex_destroy(&err_logger->lock);
free(err_logger);
return OK_RESPONSE;
}

View File

@ -37,7 +37,7 @@ od_err_logger_create_default()
}
extern od_retcode_t
od_err_logger_free(od_error_logger_t *l);
od_err_logger_free(od_error_logger_t *err_logger);
static inline od_retcode_t
od_err_logger_inc_interval(od_error_logger_t *l)

View File

@ -785,9 +785,9 @@ od_frontend_remote(od_client_t *client)
status = od_relay_step(&server->relay);
if (status == OD_DETACH) {
/* write any pending data to server first */
od_frontend_status_t status;
status = od_relay_flush(&server->relay);
if (status != OD_OK)
od_frontend_status_t flush_status;
flush_status = od_relay_flush(&server->relay);
if (flush_status != OD_OK)
break;
od_relay_detach(&client->relay);
od_relay_stop(&server->relay);
@ -795,7 +795,7 @@ od_frontend_remote(od_client_t *client)
/* cleanup server */
rc = od_reset(server);
if (rc == -1) {
status = OD_ESERVER_WRITE;
flush_status = OD_ESERVER_WRITE;
break;
}
@ -810,11 +810,11 @@ od_frontend_remote(od_client_t *client)
}
if (client->server) {
od_server_t *server = client->server;
od_server_t *curr_server = client->server;
od_frontend_status_t flush_status;
flush_status = od_relay_flush(&server->relay);
od_relay_stop(&server->relay);
flush_status = od_relay_flush(&curr_server->relay);
od_relay_stop(&curr_server->relay);
if (flush_status != OD_OK) {
return flush_status;
}

View File

@ -90,10 +90,11 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
/* validate command line options */
if (argc != 2) {
od_usage(instance, argv[0]);
return -1;
goto error;
}
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
od_usage(instance, argv[0]);
od_router_free(&router);
return 0;
}
instance->config_file = argv[1];
@ -109,18 +110,20 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
instance->config_file);
if (rc == -1) {
od_error(&instance->logger, "config", NULL, NULL, "%s", error.error);
return -1;
goto error;
}
/* validate configuration */
rc = od_config_validate(&instance->config, &instance->logger);
if (rc == -1)
return -1;
if (rc == -1) {
goto error;
}
/* validate rules */
rc = od_rules_validate(&router.rules, &instance->config, &instance->logger);
if (rc == -1)
return -1;
if (rc == -1) {
goto error;
}
/* configure logger */
od_logger_set_format(&instance->logger, instance->config.log_format);
@ -130,8 +133,9 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
/* run as daemon */
if (instance->config.daemonize) {
rc = od_daemonize();
if (rc == -1)
return -1;
if (rc == -1) {
goto error;
}
/* update pid */
od_pid_init(&instance->pid);
}
@ -146,7 +150,7 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
NULL,
"failed to open log file '%s'",
instance->config.log_file);
return -1;
goto error;
}
}
@ -202,7 +206,7 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
if (rc == -1) {
od_error(
&instance->logger, "init", NULL, NULL, "failed to init machinarium");
return -1;
goto error;
}
/* create pid file */
@ -211,8 +215,13 @@ od_instance_main(od_instance_t *instance, int argc, char **argv)
/* start system machine thread */
rc = od_system_start(&system, &global);
if (rc == -1)
return -1;
if (rc == -1) {
goto error;
}
return machine_wait(system.machine);
error:
od_router_free(&router);
return NOT_OK_RESPONSE;
}

View File

@ -42,7 +42,7 @@ od_route_init(od_route_t *route)
/* stat init */
route->stats_mark_db = false;
/* error logging*/;
/* error logging */;
route->frontend_err_logger = od_err_logger_create_default();
od_stat_init(&route->stats);

View File

@ -39,6 +39,7 @@ od_router_free(od_router_t *router)
od_route_pool_free(&router->route_pool);
od_rules_free(&router->rules);
pthread_mutex_destroy(&router->lock);
od_err_logger_free(router->router_err_logger);
}
inline int