[PATCH]fix potential memory leaks

This commit is contained in:
liuwentan
2023-10-27 17:31:35 +08:00
parent 9f7c82e6b0
commit 9b5a07ecc6
12 changed files with 29 additions and 9 deletions

View File

@@ -428,10 +428,12 @@ void *adapter_hs_new(struct expr_rule *rules, size_t n_rule,
if (literal_cd != NULL) { if (literal_cd != NULL) {
adpt_hs_compile_data_free(literal_cd); adpt_hs_compile_data_free(literal_cd);
literal_cd = NULL;
} }
if (regex_cd != NULL) { if (regex_cd != NULL) {
adpt_hs_compile_data_free(regex_cd); adpt_hs_compile_data_free(regex_cd);
regex_cd = NULL;
} }
if (hs_ret < 0) { if (hs_ret < 0) {

View File

@@ -300,10 +300,12 @@ void *adapter_rs_new(struct expr_rule *rules, size_t n_rule,
if (literal_cd != NULL) { if (literal_cd != NULL) {
adpt_rs_compile_data_free(literal_cd); adpt_rs_compile_data_free(literal_cd);
literal_cd = NULL;
} }
if (regex_cd != NULL) { if (regex_cd != NULL) {
adpt_rs_compile_data_free(regex_cd); adpt_rs_compile_data_free(regex_cd);
regex_cd = NULL;
} }
if (rs_ret < 0) { if (rs_ret < 0) {
@@ -385,6 +387,7 @@ void adapter_rs_free(void *rs_instance)
for (i = 0; i < rs_inst->n_worker_thread; i++) { for (i = 0; i < rs_inst->n_worker_thread; i++) {
if (rs_inst->rs_rt->matched_pats[i] != NULL) { if (rs_inst->rs_rt->matched_pats[i] != NULL) {
utarray_free(rs_inst->rs_rt->matched_pats[i]->pattern_ids); utarray_free(rs_inst->rs_rt->matched_pats[i]->pattern_ids);
rs_inst->rs_rt->matched_pats[i]->pattern_ids = NULL;
FREE(rs_inst->rs_rt->matched_pats[i]); FREE(rs_inst->rs_rt->matched_pats[i]);
} }
} }

View File

@@ -65,6 +65,8 @@ void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt,
struct ex_container *ex_container_new(void *ex_data, void *custom_data); struct ex_container *ex_container_new(void *ex_data, void *custom_data);
void ex_container_free(void *ex_data_runtime, void *ex_container);
/* ex_data_runtime ex data API */ /* ex_data_runtime ex data API */
void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt,
const char *table_name, const char *row, const char *table_name, const char *row,

View File

@@ -213,6 +213,8 @@ static void clear_iris_descriptor(struct iris_description *iris_cfg)
} }
maat_kv_store_free(iris_cfg->str2int_map); maat_kv_store_free(iris_cfg->str2int_map);
iris_cfg->str2int_map = NULL;
FREE(iris_cfg->encrypt_algo); FREE(iris_cfg->encrypt_algo);
FREE(iris_cfg->encrypt_key); FREE(iris_cfg->encrypt_key);

View File

@@ -250,6 +250,7 @@ static int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugi
struct ex_container *ex_container = ex_container_new(ex_data, (void *)expr); struct ex_container *ex_container = ex_container_new(ex_data, (void *)expr);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1; return -1;
} }
} }

View File

@@ -165,15 +165,16 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data)
return ex_container; return ex_container;
} }
void ex_container_free(void *user_ctx, void *data) void ex_container_free(void *ex_data_runtime, void *ex_container)
{ {
/* schema is NULL if not call ex_data_runtime_set_ex_container_schema */ /* schema is NULL if not call ex_data_runtime_set_ex_container_schema */
if (NULL == user_ctx || NULL == data) { if (NULL == ex_data_runtime || NULL == ex_container) {
return; return;
} }
struct ex_container *container = (struct ex_container *)data; struct ex_container *container = (struct ex_container *)ex_container;
struct ex_container_schema *container_schema = (struct ex_container_schema *)user_ctx; struct ex_data_runtime *ex_data_rt = (struct ex_data_runtime *)ex_data_runtime;
struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
/* free ex_container->custom_data */ /* free ex_container->custom_data */
if (container->custom_data != NULL && container_schema->custom_data_free != NULL) { if (container->custom_data != NULL && container_schema->custom_data_free != NULL) {
@@ -205,8 +206,7 @@ int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt,
if (NULL == ex_data_rt->htable) { if (NULL == ex_data_rt->htable) {
/* ex_data_rt->ref_container_schema has been set */ /* ex_data_rt->ref_container_schema has been set */
assert(ex_data_rt->ref_container_schema != NULL); assert(ex_data_rt->ref_container_schema != NULL);
ex_data_rt->htable = rcu_hash_new(ex_container_free, ex_data_rt->htable = rcu_hash_new(ex_container_free, ex_data_rt,
ex_data_rt->ref_container_schema,
ex_data_rt->gc_timeout_s); ex_data_rt->gc_timeout_s);
} }

View File

@@ -376,6 +376,7 @@ static int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugi
struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule); struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1; return -1;
} }
} }

View File

@@ -333,6 +333,7 @@ static int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule); struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1; return -1;
} }
} }

View File

@@ -382,6 +382,7 @@ static int ipport_plugin_runtime_update_row(struct ipport_plugin_runtime *ipport
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ipport_item); struct ex_container *ex_container = ex_container_new(ex_data, (void *)ipport_item);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
if (ret < 0) { if (ret < 0) {
ex_container_free(ex_data_rt, ex_container);
return -1; return -1;
} }
} }

View File

@@ -369,6 +369,7 @@ static int plugin_runtime_update_row(struct plugin_runtime *plugin_rt,
ret = ex_data_runtime_add_ex_container(plugin_rt->ex_data_rt, key, key_len, ret = ex_data_runtime_add_ex_container(plugin_rt->ex_data_rt, key, key_len,
ex_container); ex_container);
if (ret < 0) { if (ret < 0) {
ex_container_free(plugin_rt->ex_data_rt, ex_container);
return -1; return -1;
} }
plugin_rt->acc_line_num++; plugin_rt->acc_line_num++;

View File

@@ -61,6 +61,7 @@ static void rcu_hash_garbage_collect_force(struct rcu_hash_table *htable)
while ((p = TAILQ_FIRST(&(htable->garbage_q))) != NULL) { while ((p = TAILQ_FIRST(&(htable->garbage_q))) != NULL) {
p->garbage_free(p->garbage); p->garbage_free(p->garbage);
p->garbage = NULL;
TAILQ_REMOVE(&(htable->garbage_q), p, entries); TAILQ_REMOVE(&(htable->garbage_q), p, entries);
FREE(p); FREE(p);
htable->garbage_q_len--; htable->garbage_q_len--;
@@ -77,6 +78,7 @@ void rcu_hash_garbage_collect_routine(struct rcu_hash_table *htable)
if ((now - p->create_time) > htable->gc_timeout_s || if ((now - p->create_time) > htable->gc_timeout_s ||
htable->gc_timeout_s == 0) { htable->gc_timeout_s == 0) {
p->garbage_free(p->garbage); p->garbage_free(p->garbage);
p->garbage = NULL;
TAILQ_REMOVE(&(htable->garbage_q), p, entries); TAILQ_REMOVE(&(htable->garbage_q), p, entries);
FREE(p); FREE(p);
htable->garbage_q_len--; htable->garbage_q_len--;
@@ -107,6 +109,7 @@ static void rcu_hash_node_free(struct rcu_hash_node *node)
if (node->data != NULL) { if (node->data != NULL) {
node->htable->data_free_fn(node->htable->user_ctx, node->data); node->htable->data_free_fn(node->htable->user_ctx, node->data);
node->data = NULL;
} }
FREE(node); FREE(node);
} }

View File

@@ -85,6 +85,12 @@ TEST(EXDataRuntime, Update) {
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container); ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row2, key2, key2_len);
ex_container = ex_container_new(ex_data, NULL);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
EXPECT_EQ(ret, -1);
ex_container_free(ex_data_rt, ex_container);
ex_data_runtime_commit(ex_data_rt); ex_data_runtime_commit(ex_data_rt);
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, key1, key1_len); void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, key1, key1_len);
@@ -114,9 +120,6 @@ TEST(EXDataRuntime, Update) {
EXPECT_EQ(0, strcmp(info2->name, "liyanhong")); EXPECT_EQ(0, strcmp(info2->name, "liyanhong"));
EXPECT_EQ(info2->id, 102); EXPECT_EQ(info2->id, 102);
EXPECT_NE(0, strcmp(info1->name, "mahuateng"));
EXPECT_NE(info1->id, 101);
ex_data_runtime_free(ex_data_rt); ex_data_runtime_free(ex_data_rt);
FREE(container_schema); FREE(container_schema);
} }