mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-29 10:25:12 +00:00
Fix handling of the From parameter in StringRef::find.
Enable bounds checking to catch this kind of bug earlier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142247 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -153,19 +153,22 @@ size_t StringRef::find(StringRef Str, size_t From) const {
|
|||||||
return npos;
|
return npos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (From >= Length)
|
||||||
|
return npos;
|
||||||
|
|
||||||
// Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
|
// Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
|
||||||
uint8_t BadCharSkip[256];
|
uint8_t BadCharSkip[256];
|
||||||
std::memset(BadCharSkip, N, 256);
|
std::memset(BadCharSkip, N, 256);
|
||||||
for (unsigned i = 0; i != N-1; ++i)
|
for (unsigned i = 0; i != N-1; ++i)
|
||||||
BadCharSkip[(uint8_t)Str[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) {
|
while (Len >= N) {
|
||||||
if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
|
if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
|
||||||
return Pos;
|
return Pos;
|
||||||
|
|
||||||
// Otherwise skip the appropriate number of bytes.
|
// 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;
|
Len -= Skip;
|
||||||
Pos += Skip;
|
Pos += Skip;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user