lkml.org 
[lkml]   [2022]   [Jan]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[PATCH 5.16 0107/1039] libbpf: Fix section counting logic
    Date
    From: Andrii Nakryiko <andrii@kernel.org>

    [ Upstream commit 0d6988e16a12ebd41d3e268992211b0ceba44ed7 ]

    e_shnum does include section #0 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: 25bbbd7a444b ("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
    Signed-off-by: Sasha Levin <sashal@kernel.org>
    ---
    tools/lib/bpf/libbpf.c | 8 ++++----
    1 file changed, 4 insertions(+), 4 deletions(-)

    diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
    index 7c74342bb6680..5367bc8e52073 100644
    --- a/tools/lib/bpf/libbpf.c
    +++ b/tools/lib/bpf/libbpf.c
    @@ -3191,11 +3191,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
    Elf_Scn *scn;
    Elf64_Shdr *sh;

    - /* ELF section indices are 1-based, so allocate +1 element to keep
    - * indexing simple. Also include 0th invalid section into sec_cnt for
    - * simpler and more traditional iteration logic.
    + /* ELF section indices are 0-based, but sec #0 is special "invalid"
    + * section. e_shnum does include sec #0, so e_shnum is the necessary
    + * 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));
    if (!obj->efile.secs)
    return -ENOMEM;
    --
    2.34.1


    \
     
     \ /
      Last update: 2022-01-24 22:54    [W:4.465 / U:1.004 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site