[lld] r231765 - Fix a bug where the code would use subscript a std::vector with the size

Chandler Carruth chandlerc at gmail.com
Tue Mar 10 00:53:02 PDT 2015


Author: chandlerc
Date: Tue Mar 10 02:53:02 2015
New Revision: 231765

URL: http://llvm.org/viewvc/llvm-project?rev=231765&view=rev
Log:
Fix a bug where the code would use subscript a std::vector with the size
of the vector. For a vector 'v', '&v[v.size()]' isn't a valid way to
compute a pointer one-past-the-end of the vector. Instead, write the
loop in terms of iterators and save the beginning iterator. Once we have
that we can compute the beginning pointer from the beginning iterator,
and compute the distance which we should increment the beginning pointer
by subtracting the iterators.

What might be simpler would be to convert the function accepting a raw
pointer for begin and end to accept iterators or a range or some other
construct, but I wanted to keep this to a minimal bug-fix change.

This fixes a crash on any debug STL implementation which checks for
indexing out of bounds.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=231765&r1=231764&r2=231765&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Tue Mar 10 02:53:02 2015
@@ -970,13 +970,14 @@ BaseRelocChunk::createContents(ChunkVect
 
   // Base relocations for the same memory page are grouped together
   // and passed to createBaseRelocBlock.
-  for (size_t i = 0, e = relocSites.size(); i < e;) {
-    const BaseReloc *begin = &relocSites[i];
-    uint64_t pageAddr = (begin->addr & ~mask);
-    for (++i; i < e; ++i)
-      if ((relocSites[i].addr & ~mask) != pageAddr)
+  for (auto it = relocSites.begin(), e = relocSites.end(); it != e;) {
+    auto begin_it = it;
+    uint64_t pageAddr = (begin_it->addr & ~mask);
+    for (++it; it != e; ++it)
+      if ((it->addr & ~mask) != pageAddr)
         break;
-    const BaseReloc *end = &relocSites[i];
+    const BaseReloc *begin = &*begin_it;
+    const BaseReloc *end = begin + (it - begin_it);
     std::vector<uint8_t> block = createBaseRelocBlock(pageAddr, begin, end);
     contents.insert(contents.end(), block.begin(), block.end());
   }





More information about the llvm-commits mailing list