From 0aa0fb25e048c1d9abca03a8a1681b99ddcc7410 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 28 Mar 2016 17:50:28 +0200 Subject: Another round of extcap memleak fixes Fix a bunch of memory leaks, mainly because extcap_base_cleanup is not called on most execution paths and because memory allocated for options were not freed. Additionally, randpkt will now fail if no option is given (it previously returned 0 if --capture was missing). Logic using "goto" is introduced with the idea that a program should fail (ret = EXIT_FAILURE) unless proven otherwise. Now none of the extcap programs are leaking: for what in ssh cisco; do for arg in '' --help --extcap-interfaces --extcap-interface=$what; do extcap/${what}dump $arg; done; done ./tshark -D Change-Id: I6df1027ed0c32bd53fe87e6c54d355bc8ddd01f5 Reviewed-on: https://code.wireshark.org/review/14671 Petri-Dish: Peter Wu Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- extcap/ciscodump.c | 43 ++++++++++++++++++++++------------ extcap/extcap-base.c | 5 +++- extcap/randpktdump.c | 49 ++++++++++++++++++++++----------------- extcap/sshdump.c | 65 +++++++++++++++++++++++++++++----------------------- 4 files changed, 96 insertions(+), 66 deletions(-) (limited to 'extcap') diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 2eb08772fe..74df9475d5 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -565,7 +565,7 @@ int main(int argc, char **argv) char* sshkey_passphrase = NULL; char* remote_filter = NULL; unsigned long int count = 0; - int ret = EXIT_SUCCESS; + int ret = EXIT_FAILURE; extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1); #ifdef _WIN32 @@ -582,7 +582,7 @@ int main(int argc, char **argv) if (argc == 1) { help(argv[0]); - return EXIT_FAILURE; + goto end; } for (i = 0; i < argc; i++) { @@ -596,7 +596,8 @@ int main(int argc, char **argv) case OPT_HELP: help(argv[0]); - return EXIT_SUCCESS; + ret = EXIT_SUCCESS; + goto end; case OPT_VERBOSE: verbose = TRUE; @@ -604,7 +605,7 @@ int main(int argc, char **argv) case OPT_VERSION: printf("%s.%s.%s\n", CISCODUMP_VERSION_MAJOR, CISCODUMP_VERSION_MINOR, CISCODUMP_VERSION_RELEASE); - return EXIT_SUCCESS; + goto end; case OPT_REMOTE_HOST: g_free(remote_host); @@ -615,7 +616,7 @@ int main(int argc, char **argv) remote_port = (unsigned int)strtoul(optarg, NULL, 10); if (remote_port > 65535 || remote_port == 0) { printf("Invalid port: %s\n", optarg); - return EXIT_FAILURE; + goto end; } break; @@ -663,44 +664,48 @@ int main(int argc, char **argv) default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { errmsg_print("Invalid option: %s", argv[optind - 1]); - return EXIT_FAILURE; + goto end; } } } if (optind != argc) { errmsg_print("Unexpected extra option: %s", argv[optind]); - return EXIT_FAILURE; + goto end; } - if (extcap_base_handle_interface(extcap_conf)) - return EXIT_SUCCESS; + if (extcap_base_handle_interface(extcap_conf)) { + ret = EXIT_SUCCESS; + goto end; + } - if (extcap_conf->show_config) - return list_config(extcap_conf->interface, remote_port); + if (extcap_conf->show_config) { + ret = list_config(extcap_conf->interface, remote_port); + goto end; + } #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { if (verbose) errmsg_print("ERROR: WSAStartup failed with error: %d", result); - return EXIT_FAILURE; + goto end; } #endif /* _WIN32 */ if (extcap_conf->capture) { if (!remote_host) { errmsg_print("Missing parameter: --remote-host"); - return EXIT_FAILURE; + goto end; } if (!remote_interface) { errmsg_print("ERROR: No interface specified (--remote-interface)"); - return EXIT_FAILURE; + goto end; } if (count == 0) { errmsg_print("ERROR: count of packets must be specified (--remote-count)"); - return EXIT_FAILURE; + goto end; } ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, @@ -711,6 +716,14 @@ int main(int argc, char **argv) ret = EXIT_FAILURE; } +end: + g_free(remote_host); + g_free(remote_username); + g_free(remote_password); + g_free(remote_interface); + g_free(sshkey); + g_free(sshkey_passphrase); + g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; } diff --git a/extcap/extcap-base.c b/extcap/extcap-base.c index 17bf227f25..b31be5b44e 100644 --- a/extcap/extcap-base.c +++ b/extcap/extcap-base.c @@ -108,11 +108,13 @@ void extcap_base_register_interface_ext(extcap_parameters * extcap, const char * interface, const char * ifdescription, uint16_t dlt, const char * dltname, const char * dltdescription ) { - extcap_interface * iface = g_new0(extcap_interface, 1); + extcap_interface * iface; if (interface == NULL) return; + iface = g_new0(extcap_interface, 1); + iface->interface = g_strdup(interface); iface->description = g_strdup(ifdescription); iface->dlt = dlt; @@ -251,6 +253,7 @@ static void extcap_iface_free(gpointer data) g_free(iface->description); g_free(iface->dltname); g_free(iface->dltdescription); + g_free(iface); } void extcap_base_cleanup(extcap_parameters ** extcap) diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index 2b44b38381..f93b953361 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -162,6 +162,7 @@ int main(int argc, char *argv[]) randpkt_example *example; wtap_dumper* savedump; int i; + int ret = EXIT_FAILURE; #ifdef _WIN32 WSADATA wsaData; @@ -174,7 +175,7 @@ int main(int argc, char *argv[]) if (argc == 1) { help(argv[0]); - return EXIT_FAILURE; + goto end; } #ifdef _WIN32 @@ -190,7 +191,8 @@ int main(int argc, char *argv[]) switch (result) { case OPT_VERSION: printf("%s.%s.%s\n", RANDPKTDUMP_VERSION_MAJOR, RANDPKTDUMP_VERSION_MINOR, RANDPKTDUMP_VERSION_RELEASE); - return 0; + ret = EXIT_SUCCESS; + goto end; case OPT_VERBOSE: verbose = TRUE; @@ -198,13 +200,14 @@ int main(int argc, char *argv[]) case OPT_HELP: help(argv[0]); - return 0; + ret = EXIT_SUCCESS; + goto end; case OPT_MAXBYTES: maxbytes = atoi(optarg); if (maxbytes > MAXBYTES_LIMIT) { errmsg_print("randpktdump: Max bytes is %u", MAXBYTES_LIMIT); - return 1; + goto end; } break; @@ -225,6 +228,7 @@ int main(int argc, char *argv[]) break; case OPT_TYPE: + g_free(type); type = g_strdup(optarg); break; @@ -238,26 +242,30 @@ int main(int argc, char *argv[]) if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { errmsg_print("Invalid option: %s", argv[optind - 1]); - return EXIT_FAILURE; + goto end; } } } if (optind != argc) { errmsg_print("Invalid option: %s", argv[optind]); - return EXIT_FAILURE; + goto end; } - if (extcap_base_handle_interface(extcap_conf)) - return EXIT_SUCCESS; + if (extcap_base_handle_interface(extcap_conf)) { + ret = EXIT_SUCCESS; + goto end; + } - if (extcap_conf->show_config) - return list_config(extcap_conf->interface); + if (extcap_conf->show_config) { + ret = list_config(extcap_conf->interface); + goto end; + } /* Some sanity checks */ if ((random_type) && (all_random)) { errmsg_print("You can specify only one between: --random-type, --all-random"); - return EXIT_FAILURE; + goto end; } /* Wireshark sets the type, even when random options are selected. We don't want it */ @@ -271,7 +279,7 @@ int main(int argc, char *argv[]) if (result != 0) { if (verbose) errmsg_print("ERROR: WSAStartup failed with error: %d", result); - return 1; + goto end; } #endif /* _WIN32 */ @@ -279,18 +287,17 @@ int main(int argc, char *argv[]) if (g_strcmp0(extcap_conf->interface, RANDPKT_EXTCAP_INTERFACE)) { errmsg_print("ERROR: invalid interface"); - return 1; + goto end; } randpkt_seed(); if (!all_random) { produce_type = randpkt_parse_type(type); - g_free(type); example = randpkt_find_example(produce_type); if (!example) - return 1; + goto end; verbose_print("Generating packets: %s\n", example->abbrev); @@ -301,7 +308,7 @@ int main(int argc, char *argv[]) produce_type = randpkt_parse_type(NULL); example = randpkt_find_example(produce_type); if (!example) - return 1; + goto end; randpkt_example_init(example, extcap_conf->fifo, maxbytes); while (count-- > 0) { @@ -312,20 +319,20 @@ int main(int argc, char *argv[]) example = randpkt_find_example(produce_type); if (!example) - return 1; + goto end; example->dump = savedump; } randpkt_example_close(example); } + ret = EXIT_SUCCESS; } +end: /* clean up stuff */ + g_free(type); extcap_base_cleanup(&extcap_conf); - if (type) - g_free(type); - - return 0; + return ret; } #ifdef _WIN32 diff --git a/extcap/sshdump.c b/extcap/sshdump.c index 92c6a0f4fd..77dc11ad04 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -361,7 +361,7 @@ int main(int argc, char **argv) char* sshkey_passphrase = NULL; char* remote_filter = NULL; unsigned long int count = 0; - int ret = 0; + int ret = EXIT_FAILURE; extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1); #ifdef _WIN32 @@ -378,7 +378,7 @@ int main(int argc, char **argv) if (argc == 1) { help(argv[0]); - return EXIT_FAILURE; + goto end; } for (i = 0; i < argc; i++) { @@ -392,7 +392,8 @@ int main(int argc, char **argv) case OPT_HELP: help(argv[0]); - return EXIT_SUCCESS; + ret = EXIT_SUCCESS; + goto end; case OPT_VERBOSE: verbose = TRUE; @@ -400,11 +401,11 @@ int main(int argc, char **argv) case OPT_VERSION: printf("%s.%s.%s\n", SSHDUMP_VERSION_MAJOR, SSHDUMP_VERSION_MINOR, SSHDUMP_VERSION_RELEASE); - return EXIT_SUCCESS; + ret = EXIT_SUCCESS; + goto end; case OPT_REMOTE_HOST: - if (remote_host) - g_free(remote_host); + g_free(remote_host); remote_host = g_strdup(optarg); break; @@ -412,51 +413,44 @@ int main(int argc, char **argv) remote_port = (unsigned int)strtoul(optarg, NULL, 10); if (remote_port > 65535 || remote_port == 0) { errmsg_print("Invalid port: %s", optarg); - return EXIT_FAILURE; + goto end; } break; case OPT_REMOTE_USERNAME: - if (remote_username) - g_free(remote_username); + g_free(remote_username); remote_username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: - if (remote_password) - g_free(remote_password); + g_free(remote_password); remote_password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: - if (sshkey) - g_free(sshkey); + g_free(sshkey); sshkey = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: - if (sshkey_passphrase) - g_free(sshkey_passphrase); + g_free(sshkey_passphrase); sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_REMOTE_INTERFACE: - if (remote_interface) - g_free(remote_interface); + g_free(remote_interface); remote_interface = g_strdup(optarg); break; case OPT_REMOTE_CAPTURE_BIN: - if (remote_capture_bin) - g_free(remote_capture_bin); + g_free(remote_capture_bin); remote_capture_bin = g_strdup(optarg); break; case OPT_REMOTE_FILTER: - if (remote_filter) - g_free(remote_filter); + g_free(remote_filter); remote_filter = g_strdup(optarg); break; @@ -472,28 +466,32 @@ int main(int argc, char **argv) default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { errmsg_print("Invalid option: %s", argv[optind - 1]); - return EXIT_FAILURE; + goto end; } } } if (optind != argc) { errmsg_print("Unexpected extra option: %s", argv[optind]); - return EXIT_FAILURE; + goto end; } - if (extcap_base_handle_interface(extcap_conf)) - return EXIT_SUCCESS; + if (extcap_base_handle_interface(extcap_conf)) { + ret = EXIT_SUCCESS; + goto end; + } - if (extcap_conf->show_config) - return list_config(extcap_conf->interface, remote_port); + if (extcap_conf->show_config) { + ret = list_config(extcap_conf->interface, remote_port); + goto end; + } #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { if (verbose) errmsg_print("ERROR: WSAStartup failed with error: %d", result); - return EXIT_FAILURE; + goto end; } #endif /* _WIN32 */ @@ -502,7 +500,7 @@ int main(int argc, char **argv) if (!remote_host) { errmsg_print("Missing parameter: --remote-host"); - return EXIT_FAILURE; + goto end; } filter = concat_filters(extcap_conf->capture_filter, remote_filter); ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, @@ -514,7 +512,16 @@ int main(int argc, char **argv) ret = EXIT_FAILURE; } +end: /* clean up stuff */ + g_free(remote_host); + g_free(remote_username); + g_free(remote_password); + g_free(remote_interface); + g_free(remote_capture_bin); + g_free(sshkey); + g_free(sshkey_passphrase); + g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; } -- cgit v1.2.3