Commit 05d81703 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Fix memory leak when emitting final btf_ext

Free temporary allocated memory used to construct finalized .BTF.ext data.
Found by Coverity static analysis on libbpf's Github repo.

Fixes: 8fd27bf6 ("libbpf: Add BPF static linker BTF and BTF.ext support")
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20210327042502.969745-1-andrii@kernel.org
parent b83fd195
...@@ -1906,8 +1906,10 @@ static int finalize_btf_ext(struct bpf_linker *linker) ...@@ -1906,8 +1906,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i]; struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info); sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
if (sz < 0) if (sz < 0) {
return sz; err = sz;
goto out;
}
cur += sz; cur += sz;
} }
...@@ -1921,8 +1923,10 @@ static int finalize_btf_ext(struct bpf_linker *linker) ...@@ -1921,8 +1923,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i]; struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info); sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
if (sz < 0) if (sz < 0) {
return sz; err = sz;
goto out;
}
cur += sz; cur += sz;
} }
...@@ -1936,8 +1940,10 @@ static int finalize_btf_ext(struct bpf_linker *linker) ...@@ -1936,8 +1940,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i]; struct dst_sec *sec = &linker->secs[i];
sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info); sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
if (sz < 0) if (sz < 0) {
return sz; err = sz;
goto out;
}
cur += sz; cur += sz;
} }
...@@ -1948,8 +1954,10 @@ static int finalize_btf_ext(struct bpf_linker *linker) ...@@ -1948,8 +1954,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
if (err) { if (err) {
linker->btf_ext = NULL; linker->btf_ext = NULL;
pr_warn("failed to parse final .BTF.ext data: %d\n", err); pr_warn("failed to parse final .BTF.ext data: %d\n", err);
return err; goto out;
} }
return 0; out:
free(data);
return err;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment