diff mbox series

[bpf-next] selftests/bpf: Fix strncpy() fortify warning

Message ID tencent_BA460110770C008560B21A2E3555405E9F09@qq.com (mailing list archive)
State New
Headers show
Series [bpf-next] selftests/bpf: Fix strncpy() fortify warning | expand

Commit Message

Rong Tao Oct. 29, 2022, 2:59 a.m. UTC
From: Rong Tao <rongtao@cestc.cn>

Replace strncpy() with strncat(), strncat() leaves the dst string zero
terminated. Compile samples/bpf warning:

$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

David Laight Oct. 29, 2022, 12:23 p.m. UTC | #1
From: Rong Tao
> Sent: 29 October 2022 04:00
> 
> From: Rong Tao <rongtao@cestc.cn>
> 
> Replace strncpy() with strncat(), strncat() leaves the dst string zero
> terminated. Compile samples/bpf warning:

This only makes a difference because tests for strncat() have not been added.
srtncat() really is never the string function you are looking for.

	David


> 
> $ cd samples/bpf
> $ make
> ...
> In function ‘__enable_controllers’:
> samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified
> bound 4097 equals destination size [-Wstringop-truncation]
>    80 |                 strncpy(enable, controllers, sizeof(enable));
>       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Signed-off-by: Rong Tao <rongtao@cestc.cn>
> ---
>  tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c
> b/tools/testing/selftests/bpf/cgroup_helpers.c
> index a70e873b267e..912e6522c7c5 100644
> --- a/tools/testing/selftests/bpf/cgroup_helpers.c
> +++ b/tools/testing/selftests/bpf/cgroup_helpers.c
> @@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
>  		enable[len] = 0;
>  		close(fd);
>  	} else {
> -		strncpy(enable, controllers, sizeof(enable) - 1);
> +		enable[0] = '\0';
> +		strncat(enable, controllers, sizeof(enable) - 1);
>  	}
> 
>  	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
> --
> 2.31.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Rong Tao Nov. 1, 2022, 9:25 a.m. UTC | #2
Can we just use the first patch?
https://lore.kernel.org/lkml/tencent_EE3E19F80ACD66955D26A878BC768CFA210A@qq.com/
David Laight Nov. 1, 2022, 9:27 a.m. UTC | #3
From: Rong Tao <rtoax@foxmail.com>
> Sent: 01 November 2022 09:25
> 
> Can we just use the first patch?
> https://lore.kernel.org/lkml/tencent_EE3E19F80ACD66955D26A878BC768CFA210A@qq.com/

IIRC strlcpy() does the copy you want.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c
index a70e873b267e..912e6522c7c5 100644
--- a/tools/testing/selftests/bpf/cgroup_helpers.c
+++ b/tools/testing/selftests/bpf/cgroup_helpers.c
@@ -77,7 +77,8 @@  static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable) - 1);
+		enable[0] = '\0';
+		strncat(enable, controllers, sizeof(enable) - 1);
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);