[llvm-commits] [llvm] r142247 - /llvm/trunk/lib/Support/StringRef.cpp

Benjamin Kramer benny.kra at googlemail.com
Mon Oct 17 13:49:40 PDT 2011


Author: d0k
Date: Mon Oct 17 15:49:40 2011
New Revision: 142247

URL: http://llvm.org/viewvc/llvm-project?rev=142247&view=rev
Log:
Fix handling of the From parameter in StringRef::find.

Enable bounds checking to catch this kind of bug earlier.

Modified:
    llvm/trunk/lib/Support/StringRef.cpp

Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=142247&r1=142246&r2=142247&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Mon Oct 17 15:49:40 2011
@@ -153,19 +153,22 @@
     return npos;
   }
 
+  if (From >= Length)
+    return npos;
+
   // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
   uint8_t BadCharSkip[256];
   std::memset(BadCharSkip, N, 256);
   for (unsigned i = 0; i != N-1; ++i)
     BadCharSkip[(uint8_t)Str[i]] = N-1-i;
 
-  unsigned Len = Length, Pos = min(From, Length);
+  unsigned Len = Length-From, Pos = From;
   while (Len >= N) {
     if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
       return Pos;
 
     // Otherwise skip the appropriate number of bytes.
-    uint8_t Skip = BadCharSkip[(uint8_t)Data[Pos+N-1]];
+    uint8_t Skip = BadCharSkip[(uint8_t)(*this)[Pos+N-1]];
     Len -= Skip;
     Pos += Skip;
   }





More information about the llvm-commits mailing list