From 595cd45a6696361c37e9b348adb6dabdc4dfbbe9 Mon Sep 17 00:00:00 2001 From: Vy Nguyen Date: Thu, 3 Aug 2023 16:13:56 -0400 Subject: [PATCH] [lld-macho][nfc]Add bounds on sections and subsections check before attempting to dereferencing iterators. Runnign some tests with asan built of LLD would throw errors similar to the following: AddressSanitizer:DEADLYSIGNAL #0 0x55d8e6da5df7 in operator() /mnt/ssd/repo/lld/llvm-project/lld/MachO/Arch/ARM64.cpp:612 #1 0x55d8e6daa514 in operator() /mnt/ssd/repo/lld/llvm-project/lld/MachO/Arch/ARM64.cpp:650 Differential Revision: https://reviews.llvm.org/D157027 --- lld/MachO/Arch/ARM64.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp index e28e82a78cbd..e3781763c610 100644 --- a/lld/MachO/Arch/ARM64.cpp +++ b/lld/MachO/Arch/ARM64.cpp @@ -602,11 +602,15 @@ void ARM64::applyOptimizationHints(uint8_t *outBuf, const ObjFile &obj) const { addr < sectionAddr + section->getSize()) return true; + if (obj.sections.empty()) + return false; auto secIt = std::prev(llvm::upper_bound( obj.sections, addr, [](uint64_t off, const Section *sec) { return off < sec->addr; })); const Section *sec = *secIt; + if (sec->subsections.empty()) + return false; auto subsecIt = std::prev(llvm::upper_bound( sec->subsections, addr - sec->addr, [](uint64_t off, Subsection subsec) { return off < subsec.offset; }));