[llvm] r309549 - [Support/GlobPattern] - Do not crash when pattern has characters with int value < 0.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 02:26:50 PDT 2017


Author: grimar
Date: Mon Jul 31 02:26:50 2017
New Revision: 309549

URL: http://llvm.org/viewvc/llvm-project?rev=309549&view=rev
Log:
[Support/GlobPattern] - Do not crash when pattern has characters with int value < 0.

Found it during work on LLD, it would crash on following 
linker script:

SECTIONS { .foo : { *("*®") } }
That happens because ® has int value -82. And chars are used as
array index in code, and are signed by default.

Differential revision: https://reviews.llvm.org/D35891

Modified:
    llvm/trunk/lib/Support/GlobPattern.cpp
    llvm/trunk/unittests/Support/GlobPatternTest.cpp

Modified: llvm/trunk/lib/Support/GlobPattern.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/GlobPattern.cpp?rev=309549&r1=309548&r2=309549&view=diff
==============================================================================
--- llvm/trunk/lib/Support/GlobPattern.cpp (original)
+++ llvm/trunk/lib/Support/GlobPattern.cpp Mon Jul 31 02:26:50 2017
@@ -33,27 +33,30 @@ static Expected<BitVector> expand(String
     if (S.size() < 3)
       break;
 
+    uint8_t Start = S[0];
+    uint8_t End = S[2];
+
     // If it doesn't start with something like X-Y,
     // consume the first character and proceed.
     if (S[1] != '-') {
-      BV[S[0]] = true;
+      BV[Start] = true;
       S = S.substr(1);
       continue;
     }
 
     // It must be in the form of X-Y.
     // Validate it and then interpret the range.
-    if (S[0] > S[2])
+    if (Start > End)
       return make_error<StringError>("invalid glob pattern: " + Original,
                                      errc::invalid_argument);
 
-    for (int C = S[0]; C <= S[2]; ++C)
-      BV[C] = true;
+    for (int C = Start; C <= End; ++C)
+      BV[(uint8_t)C] = true;
     S = S.substr(3);
   }
 
   for (char C : S)
-    BV[C] = true;
+    BV[(uint8_t)C] = true;
   return BV;
 }
 
@@ -89,7 +92,7 @@ static Expected<BitVector> scan(StringRe
   }
   default:
     BitVector BV(256, false);
-    BV[S[0]] = true;
+    BV[(uint8_t)S[0]] = true;
     S = S.substr(1);
     return BV;
   }
@@ -159,7 +162,7 @@ bool GlobPattern::matchOne(ArrayRef<BitV
     }
 
     // If Pats[0] is not '*', it must consume one character.
-    if (S.empty() || !Pats[0][S[0]])
+    if (S.empty() || !Pats[0][(uint8_t)S[0]])
       return false;
     Pats = Pats.slice(1);
     S = S.substr(1);

Modified: llvm/trunk/unittests/Support/GlobPatternTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/GlobPatternTest.cpp?rev=309549&r1=309548&r2=309549&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/GlobPatternTest.cpp (original)
+++ llvm/trunk/unittests/Support/GlobPatternTest.cpp Mon Jul 31 02:26:50 2017
@@ -67,4 +67,13 @@ TEST_F(GlobPatternTest, Invalid) {
   EXPECT_FALSE((bool)Pat1);
   handleAllErrors(Pat1.takeError(), [&](ErrorInfoBase &EIB) {});
 }
+
+TEST_F(GlobPatternTest, ExtSym) {
+  Expected<GlobPattern> Pat1 = GlobPattern::create("a*\xFF");
+  EXPECT_TRUE((bool)Pat1);
+  EXPECT_TRUE(Pat1->match("axxx\xFF"));
+  Expected<GlobPattern> Pat2 = GlobPattern::create("[\xFF-\xFF]");
+  EXPECT_TRUE((bool)Pat2);
+  EXPECT_TRUE(Pat2->match("\xFF"));
+}
 }




More information about the llvm-commits mailing list