selftests/bpf: Fix memory leak in test selector
authorAndrii Nakryiko <andriin@fb.com>
Wed, 29 Apr 2020 01:21:05 +0000 (18:21 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 29 Apr 2020 02:48:05 +0000 (19:48 -0700)
Free test selector substrings, which were strdup()'ed.

Fixes: b65053cd94f4 ("selftests/bpf: Add whitelist/blacklist of test names to test_progs")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200429012111.277390-6-andriin@fb.com
tools/testing/selftests/bpf/test_progs.c

index b521e0a512b6e488063dc5eb087eb4514621ae6a..86d0020c9eec18a639d01e6be9d12d0ee841ff95 100644 (file)
@@ -420,6 +420,18 @@ static int libbpf_print_fn(enum libbpf_print_level level,
        return 0;
 }
 
+static void free_str_set(const struct str_set *set)
+{
+       int i;
+
+       if (!set)
+               return;
+
+       for (i = 0; i < set->cnt; i++)
+               free((void *)set->strs[i]);
+       free(set->strs);
+}
+
 static int parse_str_list(const char *s, struct str_set *set)
 {
        char *input, *state = NULL, *next, **tmp, **strs = NULL;
@@ -756,11 +768,11 @@ int main(int argc, char **argv)
        fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
                env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
 
-       free(env.test_selector.blacklist.strs);
-       free(env.test_selector.whitelist.strs);
+       free_str_set(&env.test_selector.blacklist);
+       free_str_set(&env.test_selector.whitelist);
        free(env.test_selector.num_set);
-       free(env.subtest_selector.blacklist.strs);
-       free(env.subtest_selector.whitelist.strs);
+       free_str_set(&env.subtest_selector.blacklist);
+       free_str_set(&env.subtest_selector.whitelist);
        free(env.subtest_selector.num_set);
 
        return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;