code: fix memory leak

This commit is contained in:
zhuzhenjun
2023-10-25 10:39:32 +08:00
parent 8832411f29
commit c9247d713d
7 changed files with 15 additions and 12 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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; // 精确匹配到的指纹特征

View File

@@ -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

View File

@@ -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];

View File

@@ -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);
}