Skip to content

Commit de91e63

Browse files
alan-maguireanakryiko
authored andcommitted
libbpf: bpf__find_by_name[_kind] should use btf__get_nr_types()
When operating on split BTF, btf__find_by_name[_kind] will not iterate over all types since they use btf->nr_types to show the number of types to iterate over. For split BTF this is the number of types _on top of base BTF_, so it will underestimate the number of types to iterate over, especially for vmlinux + module BTF, where the latter is much smaller. Use btf__get_nr_types() instead. Fixes: ba45136 ("libbpf: Implement basic split BTF support") Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b93ef08 commit de91e63

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tools/lib/bpf/btf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,12 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id)
674674

675675
__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
676676
{
677-
__u32 i;
677+
__u32 i, nr_types = btf__get_nr_types(btf);
678678

679679
if (!strcmp(type_name, "void"))
680680
return 0;
681681

682-
for (i = 1; i <= btf->nr_types; i++) {
682+
for (i = 1; i <= nr_types; i++) {
683683
const struct btf_type *t = btf__type_by_id(btf, i);
684684
const char *name = btf__name_by_offset(btf, t->name_off);
685685

@@ -693,12 +693,12 @@ __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
693693
__s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
694694
__u32 kind)
695695
{
696-
__u32 i;
696+
__u32 i, nr_types = btf__get_nr_types(btf);
697697

698698
if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
699699
return 0;
700700

701-
for (i = 1; i <= btf->nr_types; i++) {
701+
for (i = 1; i <= nr_types; i++) {
702702
const struct btf_type *t = btf__type_by_id(btf, i);
703703
const char *name;
704704

0 commit comments

Comments
 (0)