[lld] 159b948 - [ELF] ObjFile<ELFT>::initializeSymbols: don't call Allocate when firstGlobal==0

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 15 18:21:53 PST 2021


Author: Fangrui Song
Date: 2021-12-15T18:21:48-08:00
New Revision: 159b948e434556fa6c397066e0a13a893cc6a7d0

URL: https://github.com/llvm/llvm-project/commit/159b948e434556fa6c397066e0a13a893cc6a7d0
DIFF: https://github.com/llvm/llvm-project/commit/159b948e434556fa6c397066e0a13a893cc6a7d0.diff

LOG: [ELF] ObjFile<ELFT>::initializeSymbols: don't call Allocate when firstGlobal==0

Calling `Allocate` with 0 size (when .symtab is absent, e.g.
`invalid/mips-invalid-options-descriptor.test`) may return a nullptr, which will
crash with -fsanitize=null (the underlying `Allocate` function is
LLVM_ATTRIBUTE_RETURNS_NONNULL).

Added: 
    

Modified: 
    lld/ELF/InputFiles.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 1a1072ff5e6c..ab4be24ff82b 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1049,7 +1049,9 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
   ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
   this->symbols.resize(eSyms.size());
   SymbolUnion *locals =
-      getSpecificAllocSingleton<SymbolUnion>().Allocate(firstGlobal);
+      firstGlobal == 0
+          ? nullptr
+          : getSpecificAllocSingleton<SymbolUnion>().Allocate(firstGlobal);
 
   for (size_t i = 0; i != firstGlobal; ++i) {
     const Elf_Sym &eSym = eSyms[i];


        


More information about the llvm-commits mailing list