Commit 237c522c authored by Geliang Tang's avatar Geliang Tang Committed by Andrii Nakryiko
Browse files

selftests/bpf: Free strdup memory in test_sockmap



The strdup() function returns a pointer to a new string which is a
duplicate of the string "ptr". Memory for the new string is obtained
with malloc(), and need to be freed with free().

This patch adds these missing "free(ptr)" in check_whitelist() and
check_blacklist() to avoid memory leaks in test_sockmap.c.

Signed-off-by: default avatarGeliang Tang <tanggeliang@kylinos.cn>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/b76f2f4c550aebe4ab8ea73d23c4cbe4f06ea996.1714374022.git.tanggeliang@kylinos.cn
parent 19468ed5
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -1887,10 +1887,13 @@ static int check_whitelist(struct _test *t, struct sockmap_options *opt)
	while (entry) {
		if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
		    strstr(opt->map, entry) != 0 ||
		    strstr(t->title, entry) != 0)
		    strstr(t->title, entry) != 0) {
			free(ptr);
			return 0;
		}
		entry = strtok(NULL, ",");
	}
	free(ptr);
	return -EINVAL;
}

@@ -1907,10 +1910,13 @@ static int check_blacklist(struct _test *t, struct sockmap_options *opt)
	while (entry) {
		if ((opt->prepend && strstr(opt->prepend, entry) != 0) ||
		    strstr(opt->map, entry) != 0 ||
		    strstr(t->title, entry) != 0)
		    strstr(t->title, entry) != 0) {
			free(ptr);
			return 0;
		}
		entry = strtok(NULL, ",");
	}
	free(ptr);
	return -EINVAL;
}