From 6a75d842c9e279ed5e309e7cbc15d9ff1f81ca93 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 31 Mar 2015 22:08:43 +0000 Subject: [PATCH] ELF: Fix dereferencing end() iterator. findAbsoluteAtom() returns absoluteAtom().end() if no atom is found. Dereferencing end() value results an undefined behavior. llvm-svn: 233765 --- .../ELF/Hexagon/HexagonTargetHandler.h | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h index 6c1054757ed4..290f4af41808 100644 --- a/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h @@ -30,8 +30,7 @@ public: }; HexagonTargetLayout(HexagonLinkingContext &hti) - : TargetLayout(hti), _sdataSection(nullptr), - _gotSymAtom(nullptr), _cachedGotSymAtom(false) { + : TargetLayout(hti), _sdataSection() { _sdataSection = new (_alloc) SDataSection(hti); } @@ -84,21 +83,19 @@ public: } 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 *_sdataSection; - AtomLayout *_gotSymAtom; - bool _cachedGotSymAtom; + SDataSection *_sdataSection = nullptr; + llvm::Optional _gotSymAtom; }; /// \brief TargetHandler for Hexagon