Skip to contentSkip to conversation

Free strdup memory in selftests

PATCH Applied Geliang Tang 4 participants 8 messages Apr 29, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Free strdup memory in selftests

From: Geliang Tang <[email protected]>

Two fixes to free strdup memory in selftests to avoid memory leaks.

Geliang Tang (2):
selftests/bpf: Free strdup memory in test_sockmap selftests/bpf: Free strdup memory in veristat

 tools/testing/selftests/bpf/test_sockmap.c | 10 ++++++++--
 tools/testing/selftests/bpf/veristat.c     |  5 ++++-
 2 files changed, 12 insertions(+), 3 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/2 selftests/bpf: Free strdup memory in test_sockmap

From: Geliang Tang <[email protected]>

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.

---
 tools/testing/selftests/bpf/test_sockmap.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_sockmap.c +8 -2
index 43612de44fbf..92752f5eeded 100644
@@ -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;
 }
 
Signature · 1 line
2.40.1
reviewer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] selftests/bpf: Free strdup memory in test_sockmap

Quoted 10 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

Acked-by on v1 1/2
#3 lore raw

RE: [PATCH bpf-next 1/2] selftests/bpf: Free strdup memory in test_sockmap

In reply to Geliang Tang
Quoted 11 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

---

#4 lore raw

PATCH 2/2 selftests/bpf: Free strdup memory in veristat

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

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.

---
 tools/testing/selftests/bpf/veristat.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/veristat.c +4 -1
index 244d4996e06e..b2854238d4a0 100644
@@ -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;
 }
 
Signature · 1 line
2.40.1
reviewer Acked-by on v1 2/2
#5 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Free strdup memory in veristat

Quoted 10 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

Acked-by on v1 2/2
#6 lore raw

RE: [PATCH bpf-next 2/2] selftests/bpf: Free strdup memory in veristat

In reply to Geliang Tang
Quoted 11 lines · Geliang Tang

From: Geliang Tang <[email protected]>

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.

---

#7 lore raw
In reply to Geliang Tang
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · On Mon, 29 Apr 2024 15:07:32 +0800 you wrote:

From: Geliang Tang <[email protected]>

Two fixes to free strdup memory in selftests to avoid memory leaks.

Geliang Tang (2):
selftests/bpf: Free strdup memory in test_sockmap selftests/bpf: Free strdup memory in veristat

[...]

Here is the summary with links:
- [bpf-next,1/2] selftests/bpf: Free strdup memory in test_sockmap

    https://git.kernel.org/bpf/bpf-next/c/237c522c1d5d
  - [bpf-next,2/2] selftests/bpf: Free strdup memory in veristat
    https://git.kernel.org/bpf/bpf-next/c/25927d0a1bec

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html