diff --git a/scanner/expr_matcher/adapter_hs/adapter_hs.cpp b/scanner/expr_matcher/adapter_hs/adapter_hs.cpp index 001d160..0d3ed41 100644 --- a/scanner/expr_matcher/adapter_hs/adapter_hs.cpp +++ b/scanner/expr_matcher/adapter_hs/adapter_hs.cpp @@ -428,10 +428,12 @@ void *adapter_hs_new(struct expr_rule *rules, size_t n_rule, if (literal_cd != NULL) { adpt_hs_compile_data_free(literal_cd); + literal_cd = NULL; } if (regex_cd != NULL) { adpt_hs_compile_data_free(regex_cd); + regex_cd = NULL; } if (hs_ret < 0) { diff --git a/scanner/expr_matcher/adapter_rs/adapter_rs.cpp b/scanner/expr_matcher/adapter_rs/adapter_rs.cpp index 04e05b2..97d48f6 100644 --- a/scanner/expr_matcher/adapter_rs/adapter_rs.cpp +++ b/scanner/expr_matcher/adapter_rs/adapter_rs.cpp @@ -300,10 +300,12 @@ void *adapter_rs_new(struct expr_rule *rules, size_t n_rule, if (literal_cd != NULL) { adpt_rs_compile_data_free(literal_cd); + literal_cd = NULL; } if (regex_cd != NULL) { adpt_rs_compile_data_free(regex_cd); + regex_cd = NULL; } 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++) { if (rs_inst->rs_rt->matched_pats[i] != NULL) { 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]); } } diff --git a/src/inc_internal/maat_ex_data.h b/src/inc_internal/maat_ex_data.h index 6aa35c9..3556601 100644 --- a/src/inc_internal/maat_ex_data.h +++ b/src/inc_internal/maat_ex_data.h @@ -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); +void ex_container_free(void *ex_data_runtime, void *ex_container); + /* ex_data_runtime ex data API */ void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, const char *table_name, const char *row, diff --git a/src/json2iris.c b/src/json2iris.c index 2b3127f..7e97470 100644 --- a/src/json2iris.c +++ b/src/json2iris.c @@ -213,6 +213,8 @@ static void clear_iris_descriptor(struct iris_description *iris_cfg) } maat_kv_store_free(iris_cfg->str2int_map); + iris_cfg->str2int_map = NULL; + FREE(iris_cfg->encrypt_algo); FREE(iris_cfg->encrypt_key); diff --git a/src/maat_bool_plugin.c b/src/maat_bool_plugin.c index 86e7c9b..7064fe1 100644 --- a/src/maat_bool_plugin.c +++ b/src/maat_bool_plugin.c @@ -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); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { + ex_container_free(ex_data_rt, ex_container); return -1; } } diff --git a/src/maat_ex_data.c b/src/maat_ex_data.c index af7d4fc..13d7493 100644 --- a/src/maat_ex_data.c +++ b/src/maat_ex_data.c @@ -165,15 +165,16 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data) 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 */ - if (NULL == user_ctx || NULL == data) { + if (NULL == ex_data_runtime || NULL == ex_container) { return; } - struct ex_container *container = (struct ex_container *)data; - struct ex_container_schema *container_schema = (struct ex_container_schema *)user_ctx; + struct ex_container *container = (struct ex_container *)ex_container; + 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 */ 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) { /* ex_data_rt->ref_container_schema has been set */ assert(ex_data_rt->ref_container_schema != NULL); - ex_data_rt->htable = rcu_hash_new(ex_container_free, - ex_data_rt->ref_container_schema, + ex_data_rt->htable = rcu_hash_new(ex_container_free, ex_data_rt, ex_data_rt->gc_timeout_s); } diff --git a/src/maat_fqdn_plugin.c b/src/maat_fqdn_plugin.c index dc47f50..f5e6bc5 100644 --- a/src/maat_fqdn_plugin.c +++ b/src/maat_fqdn_plugin.c @@ -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); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { + ex_container_free(ex_data_rt, ex_container); return -1; } } diff --git a/src/maat_ip_plugin.c b/src/maat_ip_plugin.c index c94703e..48eea75 100644 --- a/src/maat_ip_plugin.c +++ b/src/maat_ip_plugin.c @@ -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); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { + ex_container_free(ex_data_rt, ex_container); return -1; } } diff --git a/src/maat_ipport_plugin.c b/src/maat_ipport_plugin.c index 4220048..afaeada 100644 --- a/src/maat_ipport_plugin.c +++ b/src/maat_ipport_plugin.c @@ -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); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { + ex_container_free(ex_data_rt, ex_container); return -1; } } diff --git a/src/maat_plugin.c b/src/maat_plugin.c index 12a1e48..eac14d6 100644 --- a/src/maat_plugin.c +++ b/src/maat_plugin.c @@ -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, ex_container); if (ret < 0) { + ex_container_free(plugin_rt->ex_data_rt, ex_container); return -1; } plugin_rt->acc_line_num++; diff --git a/src/rcu_hash.c b/src/rcu_hash.c index 7c0e052..4be9030 100644 --- a/src/rcu_hash.c +++ b/src/rcu_hash.c @@ -61,6 +61,7 @@ static void rcu_hash_garbage_collect_force(struct rcu_hash_table *htable) while ((p = TAILQ_FIRST(&(htable->garbage_q))) != NULL) { p->garbage_free(p->garbage); + p->garbage = NULL; TAILQ_REMOVE(&(htable->garbage_q), p, entries); FREE(p); 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 || htable->gc_timeout_s == 0) { p->garbage_free(p->garbage); + p->garbage = NULL; TAILQ_REMOVE(&(htable->garbage_q), p, entries); FREE(p); htable->garbage_q_len--; @@ -107,6 +109,7 @@ static void rcu_hash_node_free(struct rcu_hash_node *node) if (node->data != NULL) { node->htable->data_free_fn(node->htable->user_ctx, node->data); + node->data = NULL; } FREE(node); } diff --git a/test/maat_ex_data_gtest.cpp b/test/maat_ex_data_gtest.cpp index 07d4223..0cf2965 100644 --- a/test/maat_ex_data_gtest.cpp +++ b/test/maat_ex_data_gtest.cpp @@ -85,6 +85,12 @@ TEST(EXDataRuntime, Update) { ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container); 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); 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(info2->id, 102); - EXPECT_NE(0, strcmp(info1->name, "mahuateng")); - EXPECT_NE(info1->id, 101); - ex_data_runtime_free(ex_data_rt); FREE(container_schema); }