[PATCH] wildcard matching bug in linker script

zan jyu Wong via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 20:47:25 PDT 2015


Hi,

I've found a bug in function wildcardMatch. It failed to match `.rodata*`
with `.rodata`. I think it's cause by the early detection:

    if (i == name.end())

      return false;



And it failed to match a tailing `*` with an empty string. I have attached
a patch and a test.


Cheers,

Huang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150829/f14f3ffb/attachment.html>
-------------- next part --------------
diff --git a/test/elf/linkerscript/filename-with-wildcards.test b/test/elf/linkerscript/filename-with-wildcards.test
index 3429185..6381346 100644
--- a/test/elf/linkerscript/filename-with-wildcards.test
+++ b/test/elf/linkerscript/filename-with-wildcards.test
@@ -8,8 +8,9 @@ ENTRY(_start)
 SECTIONS
 {
   . = 0x500000;
-  .foo : { *p1.o(.text .rodata*) }
-  .bar : { *(.text .rodata*) }
+  .foo    : { *p1.o(.text .rodata*)  }
+  .bar    : { p2.o*(.text .rodata*)  }
+  .foobar : { *p3.o*(.text .rodata*) }
 }
 
 /*
@@ -31,7 +32,12 @@ CHECKSECTIONS:       Size: 33
 CHECKSECTIONS:       Index: 2
 CHECKSECTIONS:       Name: .bar
 CHECKSECTIONS:       Address: 0x500030
-CHECKSECTIONS:       Size: 52
+CHECKSECTIONS:       Size: 31
+
+CHECKSECTIONS:       Index: 3
+CHECKSECTIONS:       Name: .foobar
+CHECKSECTIONS:       Address: 0x500050
+CHECKSECTIONS:       Size: 21
 
 RUN: llvm-readobj -symbols %t1 | FileCheck -check-prefix CHECKSYMS %s
 
@@ -42,8 +48,8 @@ CHECKSYMS:      Name: prog2
 CHECKSYMS-NEXT: Value: 0x500030
 
 CHECKSYMS:      Name: write
-CHECKSYMS-NEXT: Value: 0x500040
+CHECKSYMS-NEXT: Value: 0x500050
 
 CHECKSYMS:      Name: _start
-CHECKSYMS-NEXT: Value: 0x500048
+CHECKSYMS-NEXT: Value: 0x500058
 */
-------------- next part --------------
diff --git a/lib/ReaderWriter/LinkerScript.cpp b/lib/ReaderWriter/LinkerScript.cpp
index f99a951..84ae97b 100644
--- a/lib/ReaderWriter/LinkerScript.cpp
+++ b/lib/ReaderWriter/LinkerScript.cpp
@@ -2551,9 +2551,6 @@ static bool wildcardMatch(StringRef pattern, StringRef name) {
   // Check if each char in pattern also appears in our input name, handling
   // special wildcard characters.
   for (auto j = pattern.begin(), e = pattern.end(); j != e; ++j) {
-    if (i == name.end())
-      return false;
-
     switch (*j) {
     case '*':
       while (!wildcardMatch(pattern.drop_front(j - pattern.begin() + 1),
@@ -2565,9 +2562,13 @@ static bool wildcardMatch(StringRef pattern, StringRef name) {
       break;
     case '?':
       // Matches any character
+      if (i == name.end())
+        return false;
       ++i;
       break;
     case '[': {
+      if (i == name.end())
+        return false;
       // Matches a range of characters specified between brackets
       size_t end = pattern.find(']', j - pattern.begin());
       if (end == pattern.size())
@@ -2582,12 +2583,16 @@ static bool wildcardMatch(StringRef pattern, StringRef name) {
       break;
     }
     case '\\':
+      if (i == name.end())
+        return false;
       ++j;
       if (*j != *i)
         return false;
       ++i;
       break;
     default:
+      if (i == name.end())
+        return false;
       // No wildcard character means we must match exactly the same char
       if (*j != *i)
         return false;



More information about the llvm-commits mailing list