utils: build_id: fix ubsan false positive on pointer arithmetic

get_nt_build_id() constructs a pointer by adding a base and an
offset, but if the base happens to be zero, that is undefined
under C++ rules (altough legal ELF).

Fix by performing the addition on integers, and only then
casting to a pointer.

Closes #7379
This commit is contained in:
Avi Kivity
2020-10-10 20:08:33 +03:00
committed by Nadav Har'El
parent a36eb586ea
commit af8fd8c8d8

View File

@@ -20,7 +20,7 @@ static const Elf64_Nhdr* get_nt_build_id(dl_phdr_info* info) {
continue;
}
auto* p = reinterpret_cast<const char*>(base) + h->p_vaddr;
auto* p = reinterpret_cast<const char*>(base + h->p_vaddr);
auto* e = p + h->p_memsz;
while (p != e) {
const auto* n = reinterpret_cast<const Elf64_Nhdr*>(p);