提交 55c47646 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

selftests/bpf: Fix memory leaks in btf_type_c_dump() helper

mainline inclusion
from mainline-5.17-rc1
commit 8ba28587
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8ba285874913da21ca39a46376e9cc5ce0f45f94

-------------------------------------------------

Free up memory and resources used by temporary allocated memstream and
btf_dump instance.
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Reviewed-by: NHengqi Chen <hengqi.chen@gmail.com>
Link: https://lore.kernel.org/bpf/20211107165521.9240-4-andrii@kernel.org
(cherry picked from commit 8ba28587)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 f40b3c79
...@@ -242,18 +242,23 @@ const char *btf_type_c_dump(const struct btf *btf) ...@@ -242,18 +242,23 @@ const char *btf_type_c_dump(const struct btf *btf)
d = btf_dump__new(btf, NULL, &opts, btf_dump_printf); d = btf_dump__new(btf, NULL, &opts, btf_dump_printf);
if (libbpf_get_error(d)) { if (libbpf_get_error(d)) {
fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d)); fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
return NULL; goto err_out;
} }
for (i = 1; i <= btf__get_nr_types(btf); i++) { for (i = 1; i <= btf__get_nr_types(btf); i++) {
err = btf_dump__dump_type(d, i); err = btf_dump__dump_type(d, i);
if (err) { if (err) {
fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err); fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
return NULL; goto err_out;
} }
} }
btf_dump__free(d);
fflush(buf_file); fflush(buf_file);
fclose(buf_file); fclose(buf_file);
return buf; return buf;
err_out:
btf_dump__free(d);
fclose(buf_file);
return NULL;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册