Skip to content

Commit 3ed0bc2

Browse files
theihorAlexei Starovoitov
authored andcommitted
selftests/bpf: Use strscpy in bpftool_helpers.c
Replace strncpy() calls in bpftool_helpers.c with strscpy(). Pass the destination buffer size to detect_bpftool_path() instead of hardcoding BPFTOOL_PATH_MAX_LEN. Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev> Link: https://lore.kernel.org/r/20260223190736.649171-5-ihor.solodrai@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 43277d8 commit 3ed0bc2

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

tools/testing/selftests/bpf/bpftool_helpers.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// SPDX-License-Identifier: GPL-2.0-only
2-
#include "bpftool_helpers.h"
32
#include <unistd.h>
43
#include <string.h>
54
#include <stdbool.h>
65

6+
#include "bpf_util.h"
7+
#include "bpftool_helpers.h"
8+
79
#define BPFTOOL_PATH_MAX_LEN 64
810
#define BPFTOOL_FULL_CMD_MAX_LEN 512
911

1012
#define BPFTOOL_DEFAULT_PATH "tools/sbin/bpftool"
1113

12-
static int detect_bpftool_path(char *buffer)
14+
static int detect_bpftool_path(char *buffer, size_t size)
1315
{
1416
char tmp[BPFTOOL_PATH_MAX_LEN];
1517

@@ -18,7 +20,7 @@ static int detect_bpftool_path(char *buffer)
1820
*/
1921
snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
2022
if (access(tmp, X_OK) == 0) {
21-
strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
23+
strscpy(buffer, tmp, size);
2224
return 0;
2325
}
2426

@@ -27,7 +29,7 @@ static int detect_bpftool_path(char *buffer)
2729
*/
2830
snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
2931
if (access(tmp, X_OK) == 0) {
30-
strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
32+
strscpy(buffer, tmp, size);
3133
return 0;
3234
}
3335

@@ -44,7 +46,7 @@ static int run_command(char *args, char *output_buf, size_t output_max_len)
4446
int ret;
4547

4648
/* Detect and cache bpftool binary location */
47-
if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path))
49+
if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path, sizeof(bpftool_path)))
4850
return 1;
4951

5052
ret = snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s",

0 commit comments

Comments
 (0)