diff options
author | Guy Harris <gharris@sonic.net> | 2023-02-02 23:56:26 -0800 |
---|---|---|
committer | Guy Harris <gharris@sonic.net> | 2023-02-03 08:29:44 +0000 |
commit | d69208c5e7b98dfdea3f0e51e5dc5549bfdb8d96 (patch) | |
tree | af42597cbeb87a381e62bcfdb8a313dbfb36e859 /tshark.c | |
parent | 43861fd8528b4560c89e77e7ef6738731ea3888c (diff) |
tshark: plug a memory leak.
Put the "attempt to compile a filter string as a capture filter" code
into a common routine, and, if the attempt succeeds, free up the
generated capture filter code.
Fixes #18837.
Diffstat (limited to 'tshark.c')
-rw-r--r-- | tshark.c | 51 |
1 files changed, 30 insertions, 21 deletions
@@ -758,6 +758,34 @@ must_do_dissection(dfilter_t *rfcode, dfilter_t *dfcode, tap_listeners_require_dissection() || dissect_color; } +#ifdef HAVE_LIBPCAP +/* + * Check whether a purported *shark packet-matching expression (display + * or read filter) looks like a capture filter and, if so, print a + * warning. + * + * Used, for example, if the string in question isn't a valid packet- + * matching expression. + */ +static void +warn_about_capture_filter(const char *rfilter) +{ + struct bpf_program fcode; + pcap_t *pc; + + pc = pcap_open_dead(DLT_EN10MB, MIN_PACKET_SIZE); + if (pc != NULL) { + if (pcap_compile(pc, &fcode, rfilter, 0, 0) != -1) { + pcap_freecode(&fcode); + cmdarg_err_cont( + " Note: That read filter code looks like a valid capture filter;\n" + " maybe you mixed them up?"); + } + pcap_close(pc); + } +} +#endif + int main(int argc, char *argv[]) { @@ -804,7 +832,6 @@ main(int argc, char *argv[]) int caps_queries = 0; GList *if_list; gchar *err_str, *err_str_secondary; - struct bpf_program fcode; #else gboolean capture_option_specified = FALSE; volatile int max_packet_count = 0; @@ -2092,16 +2119,7 @@ main(int argc, char *argv[]) extcap_cleanup(); #ifdef HAVE_LIBPCAP - pcap_t *pc; - pc = pcap_open_dead(DLT_EN10MB, MIN_PACKET_SIZE); - if (pc != NULL) { - if (pcap_compile(pc, &fcode, rfilter, 0, 0) != -1) { - cmdarg_err_cont( - " Note: That read filter code looks like a valid capture filter;\n" - " maybe you mixed them up?"); - } - pcap_close(pc); - } + warn_about_capture_filter(rfilter); #endif exit_status = INVALID_INTERFACE; @@ -2117,16 +2135,7 @@ main(int argc, char *argv[]) extcap_cleanup(); #ifdef HAVE_LIBPCAP - pcap_t *pc; - pc = pcap_open_dead(DLT_EN10MB, MIN_PACKET_SIZE); - if (pc != NULL) { - if (pcap_compile(pc, &fcode, dfilter, 0, 0) != -1) { - cmdarg_err_cont( - " Note: That display filter code looks like a valid capture filter;\n" - " maybe you mixed them up?"); - } - pcap_close(pc); - } + warn_about_capture_filter(rfilter); #endif exit_status = INVALID_FILTER; |