From c9247d713da668ed1bd2d332849dd9409330c0e3 Mon Sep 17 00:00:00 2001 From: zhuzhenjun Date: Wed, 25 Oct 2023 10:39:32 +0800 Subject: [PATCH] code: fix memory leak --- example/osfp_example.c | 7 +++++++ example/sample.c | 5 ++++- src/osfp.c | 11 ++--------- src/osfp_common.h | 1 - src/osfp_score_db.c | 1 + src/osfp_score_db.h | 1 - test/test.c | 1 + 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/example/osfp_example.c b/example/osfp_example.c index 7924e73..9d94015 100644 --- a/example/osfp_example.c +++ b/example/osfp_example.c @@ -25,6 +25,7 @@ #define VLAN_MAX_LAYER 2 +struct osfp_db *g_osfp_db; /* Port is just a uint16_t */ typedef uint16_t Port; @@ -496,6 +497,8 @@ void example_detect(struct osfp_db *osfp_db, Packet *p) fflush(stdout); } + free(json); + exit: if (result) { osfp_result_free(result); @@ -562,6 +565,8 @@ static void signal_handler(int signum) fflush(stdout); + osfp_db_free(g_osfp_db); + exit(0); } @@ -690,6 +695,8 @@ int main(int argc, char *argv[]) exit(1); } + g_osfp_db = osfp_db; + // loop while (1) { int r = pcap_dispatch(pcap_handle, 0, (pcap_handler)process_packet, (void*)osfp_db); diff --git a/example/sample.c b/example/sample.c index 6313c62..4c926a9 100644 --- a/example/sample.c +++ b/example/sample.c @@ -17,6 +17,7 @@ char tcph[] = { int main(int argc, char **argv) { const char *json_file_path = "./fp.json"; + char *detail_json; struct iphdr *l3_hdr = (struct iphdr *)iph; struct tcphdr *l4_hdr = (struct tcphdr *)tcph; @@ -27,7 +28,9 @@ int main(int argc, char **argv) struct osfp_result *result = osfp_ipv4_identify(db, l3_hdr, l4_hdr, l4_hdr_len); if (result) { printf("likely os: %s\n", osfp_result_os_name_get(result)); - printf("details: \n%s\n", osfp_result_score_detail_export(result)); + detail_json = osfp_result_score_detail_export(result); + printf("details: \n%s\n", detail_json); + free(detail_json); osfp_result_free(result); } osfp_db_free(db); diff --git a/src/osfp.c b/src/osfp.c index 53fa5a5..1a23342 100644 --- a/src/osfp.c +++ b/src/osfp.c @@ -97,11 +97,7 @@ char *osfp_result_score_detail_export(struct osfp_result *result) osfp_profile_get_cycle(c1); if (result == NULL) { - return NULL; - } - - if (result->json_str != NULL) { - return result->json_str; + goto exit; } root = cJSON_CreateObject(); @@ -141,7 +137,7 @@ char *osfp_result_score_detail_export(struct osfp_result *result) goto exit; } - result->json_str = result_str; + cJSON_Delete(root); osfp_profile_get_cycle(c2); osfp_profile_counter_update(&osfp_profile_result_export, c2 - c1); @@ -157,9 +153,6 @@ exit: void osfp_result_free(struct osfp_result *result) { if (result) { - if (result->json_str) { - free(result->json_str); - } free(result); } } diff --git a/src/osfp_common.h b/src/osfp_common.h index bb7319c..89da3da 100644 --- a/src/osfp_common.h +++ b/src/osfp_common.h @@ -182,7 +182,6 @@ struct osfp_result_detail { * @brief 结构体用于表示操作系统识别结果。 */ struct osfp_result { - char *json_str; // JSON 字符串 enum osfp_os_class_id likely_os_class; // 最可能的操作系统类别 struct osfp_result_detail details[OSFP_OS_CLASS_MAX]; // 详细结果数组 const char *matched; // 精确匹配到的指纹特征 diff --git a/src/osfp_score_db.c b/src/osfp_score_db.c index 60df620..00a15a3 100644 --- a/src/osfp_score_db.c +++ b/src/osfp_score_db.c @@ -408,6 +408,7 @@ static int osfp_score_db_load_entry(struct osfp_score_db *score_db, cJSON *entry } else { // TODO: same fingerprints with different os should not insert into prefilter hash table, now just tag element->repeated++; + free(fp); } // field score db diff --git a/src/osfp_score_db.h b/src/osfp_score_db.h index acb8835..8f6ef0b 100644 --- a/src/osfp_score_db.h +++ b/src/osfp_score_db.h @@ -42,7 +42,6 @@ struct osfp_score_db { unsigned int entry_count; unsigned int os_entry_count[OSFP_OS_CLASS_MAX]; - unsigned int perfect_score; unsigned int total_weight; unsigned int field_weight[OSFP_FIELD_MAX]; diff --git a/test/test.c b/test/test.c index 7a04228..c988439 100644 --- a/test/test.c +++ b/test/test.c @@ -265,6 +265,7 @@ void test_miss_rate() char *result_json = osfp_result_score_detail_export(result); if (result_json) { fprintf(log_file_ptr, "%s\n", result_json); + free(result_json); } else { fprintf(log_file_ptr, "result detail error:%p\n", result); }