public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Replace snprintf("%s", ...) with strscpy()
@ 2026-02-01 18:04 Thorsten Blum
  2026-02-01 19:26 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2026-02-01 18:04 UTC (permalink / raw)
  To: Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Thorsten Blum, bpf, linux-kernel

Replace snprintf("%s", ...) with the faster and more direct strscpy().

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 kernel/bpf/btf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 8959f3bc1e92..72fa0cb1a71b 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -25,6 +25,7 @@
 #include <linux/perf_event.h>
 #include <linux/bsearch.h>
 #include <linux/kobject.h>
+#include <linux/string.h>
 #include <linux/sysfs.h>
 #include <linux/overflow.h>
 
@@ -1210,8 +1211,7 @@ static const char *btf_show_name(struct btf_show *show)
 
 	/* Even if we don't want type name info, we want parentheses etc */
 	if (show->flags & BTF_SHOW_NONAME)
-		snprintf(show->state.name, sizeof(show->state.name), "%s",
-			 parens);
+		strscpy(show->state.name, parens);
 	else
 		snprintf(show->state.name, sizeof(show->state.name),
 			 "%s%s%s(%s%s%s%s%s%s)%s",
@@ -6324,7 +6324,7 @@ static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name
 	btf->data_size = data_size;
 	btf->kernel_btf = true;
 	btf->named_start_id = 0;
-	snprintf(btf->name, sizeof(btf->name), "%s", name);
+	strscpy(btf->name, name);
 
 	err = btf_parse_hdr(env);
 	if (err)
@@ -6443,7 +6443,7 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,
 	btf->start_str_off = base_btf->hdr.str_len;
 	btf->kernel_btf = true;
 	btf->named_start_id = 0;
-	snprintf(btf->name, sizeof(btf->name), "%s", module_name);
+	strscpy(btf->name, module_name);
 
 	btf->data = kvmemdup(data, data_size, GFP_KERNEL | __GFP_NOWARN);
 	if (!btf->data) {
-- 
Thorsten Blum <thorsten.blum@linux.dev>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next] bpf: Replace snprintf("%s", ...) with strscpy()
  2026-02-01 18:04 [PATCH bpf-next] bpf: Replace snprintf("%s", ...) with strscpy() Thorsten Blum
@ 2026-02-01 19:26 ` Alexei Starovoitov
  0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2026-02-01 19:26 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Martin KaFai Lau, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, LKML

On Sun, Feb 1, 2026 at 10:06 AM Thorsten Blum <thorsten.blum@linux.dev> wrote:
>
> Replace snprintf("%s", ...) with the faster and more direct strscpy().
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  kernel/bpf/btf.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 8959f3bc1e92..72fa0cb1a71b 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -25,6 +25,7 @@
>  #include <linux/perf_event.h>
>  #include <linux/bsearch.h>
>  #include <linux/kobject.h>
> +#include <linux/string.h>
>  #include <linux/sysfs.h>
>  #include <linux/overflow.h>
>
> @@ -1210,8 +1211,7 @@ static const char *btf_show_name(struct btf_show *show)
>
>         /* Even if we don't want type name info, we want parentheses etc */
>         if (show->flags & BTF_SHOW_NONAME)
> -               snprintf(show->state.name, sizeof(show->state.name), "%s",
> -                        parens);
> +               strscpy(show->state.name, parens);
>         else
>                 snprintf(show->state.name, sizeof(show->state.name),
>                          "%s%s%s(%s%s%s%s%s%s)%s",

I prefer to keep snprintf here for symmetry with the above.

> @@ -6324,7 +6324,7 @@ static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name
>         btf->data_size = data_size;
>         btf->kernel_btf = true;
>         btf->named_start_id = 0;
> -       snprintf(btf->name, sizeof(btf->name), "%s", name);
> +       strscpy(btf->name, name);
>
>         err = btf_parse_hdr(env);
>         if (err)
> @@ -6443,7 +6443,7 @@ static struct btf *btf_parse_module(const char *module_name, const void *data,
>         btf->start_str_off = base_btf->hdr.str_len;
>         btf->kernel_btf = true;
>         btf->named_start_id = 0;
> -       snprintf(btf->name, sizeof(btf->name), "%s", module_name);
> +       strscpy(btf->name, module_name);

These two are fine.

pw-bot: cr

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-01 19:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-01 18:04 [PATCH bpf-next] bpf: Replace snprintf("%s", ...) with strscpy() Thorsten Blum
2026-02-01 19:26 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox