[PATCH] LLD: Hexagon: Fix dereferencing end() iterator.

Rui Ueyama ruiu at google.com
Tue Mar 31 13:52:15 PDT 2015


Hi shankarke,

findAbsoluteAtom() returns absoluteAtom().end() if no atom is found.
Dereferencing end() value results an undefined behavior.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8748

Files:
  lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h

Index: lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
===================================================================
--- lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
+++ lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
@@ -30,8 +30,7 @@
   };
 
   HexagonTargetLayout(HexagonLinkingContext &hti)
-      : TargetLayout<HexagonELFType>(hti), _sdataSection(nullptr),
-        _gotSymAtom(nullptr), _cachedGotSymAtom(false) {
+      : TargetLayout<HexagonELFType>(hti), _sdataSection() {
     _sdataSection = new (_alloc) SDataSection<HexagonELFType>(hti);
   }
 
@@ -84,21 +83,19 @@
   }
 
   uint64_t getGOTSymAddr() {
-    if (!_cachedGotSymAtom) {
-      auto gotAtomIter = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
-      _gotSymAtom = (*gotAtomIter);
-      _cachedGotSymAtom = true;
+    if (!_gotSymAtom.hasValue()) {
+      auto iter = this->findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_");
+      _gotSymAtom = (iter == this->absoluteAtoms().end()) ? nullptr : *iter;
     }
-    if (_gotSymAtom)
-      return _gotSymAtom->_virtualAddr;
+    if (*_gotSymAtom)
+      return (*_gotSymAtom)->_virtualAddr;
     return 0;
   }
 
 private:
   llvm::BumpPtrAllocator _alloc;
-  SDataSection<HexagonELFType> *_sdataSection;
-  AtomLayout *_gotSymAtom;
-  bool _cachedGotSymAtom;
+  SDataSection<HexagonELFType> *_sdataSection = nullptr;
+  llvm::Optional<AtomLayout *> _gotSymAtom;
 };
 
 /// \brief TargetHandler for Hexagon

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8748.23007.patch
Type: text/x-patch
Size: 1448 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150331/c0213391/attachment.bin>


More information about the llvm-commits mailing list