diff options
author | Vitaly Lavrov <vel21ripn@gmail.com> | 2021-06-23 10:04:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-23 12:04:03 +0200 |
commit | 2234b97149b4a7a9dcdc5d04e0e25b36f6360532 (patch) | |
tree | 53404329f9a3a79251a4b17effe077521ec4681f | |
parent | 35fc6a6de5ab1be3cc4c3c1eb9428b2312a503d0 (diff) |
ndpiReader: memory leak (#1215)
Non-critical bugs.
If a file list is used, then all files except the last are not closed.
Opening the next file loses the memory allocated via pcap_open_offline() for the previous file.
If a bpf filter is used, then no memory is freed after pcap_compile.
-rw-r--r-- | example/ndpiReader.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index b4153c8bf..e04d5b452 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -98,6 +98,7 @@ static struct timeval startup_time, begin, end; static int core_affinity[MAX_NUM_READER_THREADS]; #endif static struct timeval pcap_start = { 0, 0}, pcap_end = { 0, 0 }; +static struct bpf_program bpf_code,*bpf_cfilter = NULL; /** Detection parameters **/ static time_t capture_for = 0; static time_t capture_until = 0; @@ -3138,14 +3139,17 @@ next_line: static void configurePcapHandle(pcap_t * pcap_handle) { if(bpfFilter != NULL) { - struct bpf_program fcode; - if(pcap_compile(pcap_handle, &fcode, bpfFilter, 1, 0xFFFFFF00) < 0) { - printf("pcap_compile error: '%s'\n", pcap_geterr(pcap_handle)); - } else { - if(pcap_setfilter(pcap_handle, &fcode) < 0) { + if(!bpf_cfilter) { + if(pcap_compile(pcap_handle, &bpf_code, bpfFilter, 1, 0xFFFFFF00) < 0) { + printf("pcap_compile error: '%s'\n", pcap_geterr(pcap_handle)); + return; + } + bpf_cfilter = &bpf_code; + } + if(pcap_setfilter(pcap_handle, bpf_cfilter) < 0) { printf("pcap_setfilter error: '%s'\n", pcap_geterr(pcap_handle)); - } else + } else { printf("Successfully set BPF filter to '%s'\n", bpfFilter); } } @@ -3429,6 +3433,8 @@ void * processing_thread(void *_thread_id) { pcap_loop: runPcapLoop(thread_id); + pcap_close(ndpi_thread_info[thread_id].workflow->pcap_handle); + ndpi_thread_info[thread_id].workflow->pcap_handle = NULL; if(playlist_fp[thread_id] != NULL) { /* playlist: read next file */ char filename[256]; @@ -3439,6 +3445,10 @@ pcap_loop: } } #endif + if(bpf_cfilter) { + pcap_freecode(bpf_cfilter); + bpf_cfilter = NULL; + } return NULL; } |