Skip to contentSkip to conversation

bpf: Replace snprintf("%s", ...) with strscpy()

PATCH Superseded Thorsten Blum 2 participants 2 messages Feb 1 BPF

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Replace snprintf("%s", ...) with strscpy()

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

---
 kernel/bpf/btf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/btf.c +4 -4
index 8959f3bc1e92..72fa0cb1a71b 100644
@@ -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) {
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
#1 lore raw
Quoted 21 lines · Thorsten Blum

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

---
 kernel/bpf/btf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/btf.c +1 -0
index 8959f3bc1e92..72fa0cb1a71b 100644
@@ -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>
 

kernel/bpf/btf.c static const char *btf_show_name(struct btf_show *show) · line 1217

Quoted 9 lines
kernel/bpf/btf.c +1 -2
@@ -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.

Quoted 10 lines
kernel/bpf/btf.c +1 -1
@@ -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)

kernel/bpf/btf.c static struct btf *btf_parse_module(const char *module_name, const void *data, · line 6446

Quoted 5 lines
kernel/bpf/btf.c +1 -1
@@ -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