mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] libbpf: Initialize st_ops->tname with strdup()
@ 2024-08-21 11:23 Soma Nakata
  2024-08-21 12:15 ` Christophe JAILLET
  0 siblings, 1 reply; 4+ messages in thread
From: Soma Nakata @ 2024-08-21 11:23 UTC (permalink / raw)
  To: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: soma.nakata01, bpf, linux-kernel

`tname` is returned by `btf__name_by_offset()` as well as `var_name`,
and these addresses point to strings in the btf. Since their locations
may change while loading the bpf program, using `strdup()` ensures
`tname` is safely stored.

Signed-off-by: Soma Nakata <soma.nakata01@gmail.com>
---
 tools/lib/bpf/libbpf.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a3be6f8fac09..f4ad1b993ec5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -496,7 +496,7 @@ struct bpf_program {
 };
 
 struct bpf_struct_ops {
-	const char *tname;
+	char *tname;
 	const struct btf_type *type;
 	struct bpf_program **progs;
 	__u32 *kern_func_off;
@@ -1423,7 +1423,9 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
 		memcpy(st_ops->data,
 		       data->d_buf + vsi->offset,
 		       type->size);
-		st_ops->tname = tname;
+		st_ops->tname = strdup(tname);
+		if (!st_ops->tname)
+			return -ENOMEM;
 		st_ops->type = type;
 		st_ops->type_id = type_id;
 
@@ -8984,6 +8986,7 @@ static void bpf_map__destroy(struct bpf_map *map)
 	map->mmaped = NULL;
 
 	if (map->st_ops) {
+		zfree(&map->st_ops->tname);
 		zfree(&map->st_ops->data);
 		zfree(&map->st_ops->progs);
 		zfree(&map->st_ops->kern_func_off);
-- 
2.46.0


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

end of thread, other threads:[~2024-08-21 13:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-21 11:23 [PATCH v2] libbpf: Initialize st_ops->tname with strdup() Soma Nakata
2024-08-21 12:15 ` Christophe JAILLET
2024-08-21 13:30   ` Soma Nakata
2024-08-21 13:41     ` Christophe JAILLET

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

all inboxes | Powered by JetHome®