diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-17 20:49:40 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-17 20:49:40 +0000 |
commit | e664de33b1053cd921e60152f23a5b1dd82e9552 (patch) | |
tree | 1179c46858290bc7bbb1c9ddac80fc9b5858aa5f /llvm/lib/Support/StringRef.cpp | |
parent | 15b1a7be1ef56894e28787af60fc725af7554953 (diff) | |
download | bcm5719-llvm-e664de33b1053cd921e60152f23a5b1dd82e9552.tar.gz bcm5719-llvm-e664de33b1053cd921e60152f23a5b1dd82e9552.zip |
Fix handling of the From parameter in StringRef::find.
Enable bounds checking to catch this kind of bug earlier.
llvm-svn: 142247
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index a862ed2fa9c..576b95f6a4e 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -153,19 +153,22 @@ size_t StringRef::find(StringRef Str, size_t From) const { 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; } |