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/randpktdump.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'extcap/randpktdump.c') 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 -- cgit v1.2.1