test/unit/app_ut: fix potential leak of memory

This patch fixes potential memory leak in spdk_app_parse_args() when
white or blacklist of devices is defined.

Change-Id: Ia586d77c67dbe6c664447f8431e1a7a30d624ae1
Signed-off-by: Tomasz Kulasek <tomaszx.kulasek@intel.com>
Reviewed-on: https://review.gerrithub.io/c/440982
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
This commit is contained in:
Tomasz Kulasek 2019-01-17 15:13:00 +01:00 committed by Darek Stojaczyk
parent 59b0373e09
commit 470a13301c

View File

@ -62,6 +62,16 @@ unittest_parse_args(int ch, char *arg)
return 0;
}
static void
clean_opts(struct spdk_app_opts *opts)
{
free(opts->pci_whitelist);
opts->pci_whitelist = NULL;
free(opts->pci_blacklist);
opts->pci_blacklist = NULL;
memset(opts, 0, sizeof(struct spdk_app_opts));
}
static void
test_spdk_app_parse_args(void)
{
@ -109,24 +119,28 @@ test_spdk_app_parse_args(void)
rc = spdk_app_parse_args(test_argc, valid_argv, &opts, "", NULL, unittest_parse_args, NULL);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
optind = 1;
clean_opts(&opts);
/* Test invalid short option Expected result: FAIL */
rc = spdk_app_parse_args(test_argc, argv_added_short_opt, &opts, "", NULL, unittest_parse_args,
NULL);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
optind = 1;
clean_opts(&opts);
/* Test valid global and local options. Expected result: PASS */
rc = spdk_app_parse_args(test_argc, argv_added_short_opt, &opts, "z", NULL, unittest_parse_args,
unittest_usage);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
optind = 1;
clean_opts(&opts);
/* Test invalid long option Expected result: FAIL */
rc = spdk_app_parse_args(test_argc, argv_added_long_opt, &opts, "", NULL, unittest_parse_args,
NULL);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
optind = 1;
clean_opts(&opts);
/* Test valid global and local options. Expected result: PASS */
my_options[0].name = "test-long-opt";
@ -134,23 +148,27 @@ test_spdk_app_parse_args(void)
unittest_usage);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_SUCCESS);
optind = 1;
clean_opts(&opts);
/* Test overlapping global and local options. Expected result: FAIL */
rc = spdk_app_parse_args(test_argc, valid_argv, &opts, SPDK_APP_GETOPT_STRING, NULL,
unittest_parse_args, NULL);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
optind = 1;
clean_opts(&opts);
/* 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);
SPDK_CU_ASSERT_FATAL(rc == SPDK_APP_PARSE_ARGS_FAIL);
optind = 1;
clean_opts(&opts);
/* Omit necessary argument to option */
rc = spdk_app_parse_args(test_argc, invalid_argv_missing_option, &opts, "", NULL,
unittest_parse_args, NULL);
CU_ASSERT_EQUAL(rc, SPDK_APP_PARSE_ARGS_FAIL);
optind = 1;
clean_opts(&opts);
}
int