[lld] r276741 - [ELF] - Fixed possible iterator overflow.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 26 03:47:10 PDT 2016


Author: grimar
Date: Tue Jul 26 05:47:09 2016
New Revision: 276741

URL: http://llvm.org/viewvc/llvm-project?rev=276741&view=rev
Log:
[ELF] - Fixed possible iterator overflow.

We can have Opt.Commands size greater then Sections.size().
For example if we have next script:

SECTIONS { 
.aaa : { *(.aaa) }           
.bbb : { *(.bbb) }   
.ccc : { *(.ccc) }   
}

and next code:

.global _start
_start:
 nop

.section .aaa,"a"
 .quad 0

Then amount of sections is less than amound of Opt.Commands
and if we for example have all commands NoConstraint,
that overflowed the iterator used.

Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=276741&r1=276740&r2=276741&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Jul 26 05:47:09 2016
@@ -150,19 +150,21 @@ LinkerScript<ELFT>::createSections(Outpu
 template <class ELFT>
 std::vector<OutputSectionBase<ELFT> *>
 LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
-  // Sections and OutputSectionCommands are parallel arrays.
   // In this loop, we remove output sections if they don't satisfy
   // requested properties.
-  auto It = Sections.begin();
   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
     if (!Cmd || Cmd->Name == "/DISCARD/")
       continue;
 
-    if (Cmd->Constraint == ConstraintKind::NoConstraint) {
-      ++It;
+    if (Cmd->Constraint == ConstraintKind::NoConstraint)
+      continue;
+
+    auto It = llvm::find_if(Sections, [&](OutputSectionBase<ELFT> *S) {
+      return S->getName() == Cmd->Name;
+    });
+    if (It == Sections.end())
       continue;
-    }
 
     OutputSectionBase<ELFT> *Sec = *It;
     bool Writable = (Sec->getFlags() & SHF_WRITE);
@@ -173,7 +175,6 @@ LinkerScript<ELFT>::filter(std::vector<O
       Sections.erase(It);
       continue;
     }
-    ++It;
   }
   return Sections;
 }




More information about the llvm-commits mailing list