1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00

libbpf: Fix section counting logic

e_shnum does include section  and as such is exactly the number of ELF
sections that we need to allocate memory for to use section indices as
array indices. Fix the off-by-one error.

This is purely accounting fix, previously we were overallocating one
too many array items. But no correctness errors otherwise.

Fixes: 25bbbd7a44 ("libbpf: Remove assumptions about uniqueness of .rodata/.data/.bss maps")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20211103173213.1376990-5-andrii@kernel.org
This commit is contained in:
Andrii Nakryiko 2021-11-03 10:32:12 -07:00 committed by Alexei Starovoitov
parent 62554d52e7
commit 0d6988e16a

View file

@ -3190,11 +3190,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
Elf_Scn *scn; Elf_Scn *scn;
Elf64_Shdr *sh; Elf64_Shdr *sh;
/* ELF section indices are 1-based, so allocate +1 element to keep /* ELF section indices are 0-based, but sec #0 is special "invalid"
* indexing simple. Also include 0th invalid section into sec_cnt for * section. e_shnum does include sec #0, so e_shnum is the necessary
* simpler and more traditional iteration logic. * size of an array to keep all the sections.
*/ */
obj->efile.sec_cnt = 1 + obj->efile.ehdr->e_shnum; obj->efile.sec_cnt = obj->efile.ehdr->e_shnum;
obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
if (!obj->efile.secs) if (!obj->efile.secs)
return -ENOMEM; return -ENOMEM;