From 8befeab1b4cd3b8fc4385fe894414c155d5adbb2 Mon Sep 17 00:00:00 2001 From: Tomasz Kulasek Date: Thu, 17 Jan 2019 15:13:00 +0100 Subject: [PATCH] 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 Reviewed-on: https://review.gerrithub.io/c/440982 (master) Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/447456 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker --- test/unit/lib/event/app.c/app_ut.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/lib/event/app.c/app_ut.c b/test/unit/lib/event/app.c/app_ut.c index 5b420aedd..35380ea28 100644 --- a/test/unit/lib/event/app.c/app_ut.c +++ b/test/unit/lib/event/app.c/app_ut.c @@ -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