selftests/bpf: Free strdup memory in veristat
authorGeliang Tang <tanggeliang@kylinos.cn>
Mon, 29 Apr 2024 07:07:34 +0000 (15:07 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 29 Apr 2024 23:17:15 +0000 (16:17 -0700)
The strdup() function returns a pointer to a new string which is a
duplicate of the string "input". Memory for the new string is obtained
with malloc(), and need to be freed with free().

This patch adds these missing "free(input)" in parse_stats() to avoid
memory leak in veristat.c.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/ded44f8865cd7f337f52fc5fb0a5fbed7d6bd641.1714374022.git.tanggeliang@kylinos.cn
tools/testing/selftests/bpf/veristat.c

index 244d4996e06efe3eb2e9af7cd1d8a7700f3e6082..b2854238d4a0eba35606b0b60a8298f03384f767 100644 (file)
@@ -792,10 +792,13 @@ static int parse_stats(const char *stats_str, struct stat_specs *specs)
 
        while ((next = strtok_r(state ? NULL : input, ",", &state))) {
                err = parse_stat(next, specs);
-               if (err)
+               if (err) {
+                       free(input);
                        return err;
+               }
        }
 
+       free(input);
        return 0;
 }