From d70941c3d5f73fb01906b0e5d86e462c96ad0781 Mon Sep 17 00:00:00 2001 From: Seth Howell Date: Tue, 4 Sep 2018 08:42:55 -0700 Subject: [PATCH] app: don't leak memory after failed arg parsing This is in response to a scan-build error introduced with clang 6.0 Change-Id: Iee5a2538ec9a6575e5b3087bf43b8ded5d099fe7 Signed-off-by: Seth Howell Reviewed-on: https://review.gerrithub.io/424576 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto Chandler-Test-Pool: SPDK Automated Test System --- lib/event/app.c | 10 ++++++++++ test/unit/lib/event/app.c/app_ut.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/event/app.c b/lib/event/app.c index cba61163e..86b735bc3 100644 --- a/lib/event/app.c +++ b/lib/event/app.c @@ -862,6 +862,7 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, case PCI_BLACKLIST_OPT_IDX: if (opts->pci_whitelist) { free(opts->pci_whitelist); + opts->pci_whitelist = NULL; fprintf(stderr, "-B and -W cannot be used at the same time\n"); usage(app_usage); goto out; @@ -870,6 +871,7 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, rc = spdk_app_opts_add_pci_addr(opts, &opts->pci_blacklist, optarg); if (rc != 0) { free(opts->pci_blacklist); + opts->pci_blacklist = NULL; goto out; } break; @@ -895,6 +897,7 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, case PCI_WHITELIST_OPT_IDX: if (opts->pci_blacklist) { free(opts->pci_blacklist); + opts->pci_blacklist = NULL; fprintf(stderr, "-B and -W cannot be used at the same time\n"); usage(app_usage); goto out; @@ -903,6 +906,7 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, rc = spdk_app_opts_add_pci_addr(opts, &opts->pci_whitelist, optarg); if (rc != 0) { free(opts->pci_whitelist); + opts->pci_whitelist = NULL; goto out; } break; @@ -928,6 +932,12 @@ spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, retval = SPDK_APP_PARSE_ARGS_SUCCESS; out: + if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) { + free(opts->pci_blacklist); + opts->pci_blacklist = NULL; + free(opts->pci_whitelist); + opts->pci_whitelist = NULL; + } free(cmdline_short_opts); free(cmdline_options); return retval; diff --git a/test/unit/lib/event/app.c/app_ut.c b/test/unit/lib/event/app.c/app_ut.c index 4e2dba87a..7d549261a 100644 --- a/test/unit/lib/event/app.c/app_ut.c +++ b/test/unit/lib/event/app.c/app_ut.c @@ -152,7 +152,7 @@ test_spdk_app_parse_args(void) /* Specify -B and -W options at the same time. Expected result: FAIL */ rc = spdk_app_parse_args(test_argc, invalid_argv_BW, &opts, "", NULL, unittest_parse_args, NULL); - CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL); + SPDK_CU_ASSERT_FATAL(rc == SPDK_APP_PARSE_ARGS_FAIL); optind = 1; /* Omit necessary argument to option */