Skip to contentSkip to conversation

selftests/bpf: fix a test for snprintf() overflow

PATCH Applied Dan Carpenter 3 participants 3 messages Jul 19, 2022

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

PATCH 1/1 selftests/bpf: fix a test for snprintf() overflow

The snprintf() function returns the number of bytes which *would* have been copied if there were space. In other words, it can be

Quoted 1 line

sizeof(pin_path).

Fixes c0fa1b6c3efc ("bpf: btf: Add BTF tests")
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/btf.c +1 -1
index 941b0100bafa..ef6528b8084c 100644
@@ -5338,7 +5338,7 @@ static void do_test_pprint(int test_num)
 	ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
 		       "/sys/fs/bpf", test->map_name);
 
-	if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
+	if (CHECK(ret >= sizeof(pin_path), "pin_path %s/%s is too long",
 		  "/sys/fs/bpf", test->map_name)) {
 		err = -1;
 		goto done;
Signature · 1 line
2.35.1
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Dan Carpenter

The snprintf() function returns the number of bytes which *would* have been copied if there were space. In other words, it can be

Quoted 1 line

sizeof(pin_path).

The current test does not fail on this check,
so this could be bpf-next.

#2 lore raw
In reply to Dan Carpenter
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Tue, 19 Jul 2022 12:50:32 +0300 you wrote:

The snprintf() function returns the number of bytes which *would* have been copied if there were space. In other words, it can be

Quoted 1 line

sizeof(pin_path).

Fixes c0fa1b6c3efc ("bpf: btf: Add BTF tests")

[...]

Here is the summary with links:
- selftests/bpf: fix a test for snprintf() overflow

    https://git.kernel.org/bpf/bpf-next/c/c5d22f4cfe8d

You are awesome, thank you!

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