[PATCH 5/5] core, libctf: Check if constructor arguments are NULL before using them

Arnaldo Carvalho de Melo
2024-11-19T13:41:05Z

From: Arnaldo Carvalho de Melo <acme@redhat.com>

# pahole
Segmentation fault (core dumped)
#

Now we have:

# ls -la bla
ls: cannot access 'bla': No such file or directory
# export PAHOLE_VMLINUX_BTF_FILENAME=bla
# pahole
pahole: couldn't find any debug information on this system.
# pahole list_head
pahole: couldn't find any debug information on this system.
# pahole -F btf list_head
pahole: couldn't find any btf debug information on this system.
# pahole -F dwarf list_head
pahole: couldn't find any dwarf debug information on this system.
# tests/default_vmlinux_btf.sh
Default BTF on a system without BTF: Ok
#

Reported-by: Matthias Schwarzott <zzam@gentoo.org>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
dwarves.c | 3 +++
libctf.c | 3 +++
2 files changed, 6 insertions(+)

diff --git a/dwarves.c b/dwarves.c
index 459311e64c8b45b2..ae512b9f46ddcc0a 100644
--- a/dwarves.c
+++ b/dwarves.c
@@ -777,6 +777,9 @@ struct cu *cu__new(const char *name, uint8_t addr_size,
if (cu->use_obstack)
obstack_init(&cu->obstack);

+ if (name == NULL || filename == NULL)
+ goto out_free;
+
cu->name = strdup(name);
if (cu->name == NULL)
goto out_free;
diff --git a/libctf.c b/libctf.c
index edb34dbf10de09e7..0641b10586527a6e 100644
--- a/libctf.c
+++ b/libctf.c
@@ -179,6 +179,9 @@ struct ctf *ctf__new(const char *filename, Elf *elf)
struct ctf *ctf = zalloc(sizeof(*ctf));

if (ctf != NULL) {
+ if (filename == NULL)
+ goto out_delete;
+
ctf->filename = strdup(filename);
if (ctf->filename == NULL)
goto out_delete;
--
2.47.0