[PATCH] D36307: [Support/GlobPattern] - Treat multiple stars('*') as single.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 4 05:02:15 PDT 2017


grimar created this revision.

Found that when fuzzed LLD's linkerscripts.
Next one takes long time to run for me (several seconds), causing
fuzzer to treat test as 'hang':

  SECTIONS {    .foo : { *(********************************************boo) }   }

That happens because complexity grows too fast when there are multiple
stars in pattern, though I believe we can just convert all `** to `*`.

Testcase is provided. With patch it runs instantly, without - takes huge time,


https://reviews.llvm.org/D36307

Files:
  lib/Support/GlobPattern.cpp
  unittests/Support/GlobPatternTest.cpp


Index: unittests/Support/GlobPatternTest.cpp
===================================================================
--- unittests/Support/GlobPatternTest.cpp
+++ unittests/Support/GlobPatternTest.cpp
@@ -76,4 +76,15 @@
   EXPECT_TRUE((bool)Pat2);
   EXPECT_TRUE(Pat2->match("\xFF"));
 }
+
+// Check this test does not take ages to complete.
+TEST_F(GlobPatternTest, MultipleStarts) {
+  std::string Stars(1024, '*');
+  Stars += 'X';
+  std::string Stars2(1024, 'A');
+
+  Expected<GlobPattern> Pat = GlobPattern::create(Stars);
+  EXPECT_TRUE((bool)Pat);
+  EXPECT_FALSE(Pat->match(Stars2));
+}
 }
Index: lib/Support/GlobPattern.cpp
===================================================================
--- lib/Support/GlobPattern.cpp
+++ lib/Support/GlobPattern.cpp
@@ -151,7 +151,9 @@
     // If Pats[0] is '*', try to match Pats[1..] against all possible
     // tail strings of S to see at least one pattern succeeds.
     if (Pats[0].size() == 0) {
-      Pats = Pats.slice(1);
+      // '**' is efficiency the same as '*', skip them to optimize.
+      while (!Pats.empty() && Pats.front().empty())
+        Pats = Pats.slice(1);
       if (Pats.empty())
         // Fast path. If a pattern is '*', it matches anything.
         return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36307.109709.patch
Type: text/x-patch
Size: 1244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170804/94f8bd00/attachment.bin>


More information about the llvm-commits mailing list