Skip to content

Commit b116253

Browse files
akroshgpleath
authored andcommitted
1 parent 34fa597 commit b116253

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/Runtime/Library/JavascriptString.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,7 @@ namespace Js
34153415

34163416
int JavascriptString::LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, charcount_t len, const char16* searchStr, charcount_t searchLen, charcount_t position)
34173417
{
3418+
Assert(searchLen > 0);
34183419
const char16 searchFirst = searchStr[0];
34193420
uint32 lMatchedJump = searchLen;
34203421
if (jmpTable[searchFirst].shift > 0)
@@ -3423,31 +3424,41 @@ namespace Js
34233424
}
34243425
WCHAR c;
34253426
char16 const * p = inputStr + min(len - searchLen, position);
3426-
while(p >= inputStr)
3427+
3428+
while (true)
34273429
{
3430+
uint32 remaining = (uint32)(p - inputStr);
3431+
uint32 backwardOffset = 0;
34283432
// first character match, keep checking
34293433
if (*p == searchFirst)
34303434
{
3431-
if ( wmemcmp(p, searchStr, searchLen) == 0 )
3435+
if (wmemcmp(p, searchStr, searchLen) == 0)
34323436
{
3433-
break;
3437+
return (int)remaining;
34343438
}
3435-
p -= lMatchedJump;
3439+
backwardOffset = lMatchedJump;
34363440
}
34373441
else
34383442
{
34393443
c = *p;
3440-
if ( 0 == ( c & ~0x7f ) && jmpTable[c].shift != 0 )
3444+
if (0 == (c & ~0x7f) && jmpTable[c].shift != 0)
34413445
{
3442-
p -= jmpTable[c].shift;
3446+
backwardOffset = jmpTable[c].shift;
34433447
}
34443448
else
34453449
{
3446-
p -= searchLen;
3450+
backwardOffset = searchLen;
34473451
}
34483452
}
3453+
AssertOrFailFast(backwardOffset > 0);
3454+
if (backwardOffset > remaining)
3455+
{
3456+
break;
3457+
}
3458+
p -= backwardOffset;
34493459
}
3450-
return ((p >= inputStr) ? (int)(p - inputStr) : -1);
3460+
3461+
return -1;
34513462
}
34523463

34533464
bool JavascriptString::BuildLastCharForwardBoyerMooreTable(JmpTable jmpTable, const char16* searchStr, int searchLen)

0 commit comments

Comments
 (0)