Ignore:
Timestamp:
Aug 17, 2010, 4:05:50 PM (15 years ago)
Author:
[email protected]
Message:

Bug 44099 - REGRESSION(r65468): Crashes in StringImpl::find

Reviewed by Sam Weinig.

Bug 44080 introuduced a couple of cases in which array bounds could be overrun.
One of these was fixed in r65493, this patch fixes the other and address the
concerns voiced in comment #6 by restructuring the loops to remove the code
dupliction without introducing an additional if check.

  • wtf/text/StringImpl.cpp:

(WTF::StringImpl::find):
(WTF::StringImpl::findIgnoringCase):
(WTF::StringImpl::reverseFind):
(WTF::StringImpl::reverseFindIgnoringCase):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/text/StringImpl.cpp

    r65493 r65571  
    543543    }
    544544
    545     for (unsigned i = 0; i < delta; ++i) {
    546         if (searchHash == matchHash && equal(searchCharacters + i, matchString, matchLength))
    547             return index + i;
     545    unsigned i = 0;
     546    // keep looping until we match
     547    while (searchHash != matchHash || !equal(searchCharacters + i, matchString, matchLength)) {
     548        if (i == delta)
     549            return notFound;
    548550        searchHash += searchCharacters[i + matchLength];
    549551        searchHash -= searchCharacters[i];
    550     }
    551     if (searchHash == matchHash && equal(searchCharacters + delta, matchString, matchLength))
    552         return index + delta;
    553     return notFound;
     552        ++i;
     553    }
     554    return index + i;
    554555}
    555556
     
    574575    const UChar* searchCharacters = characters() + index;
    575576
    576     for (unsigned i = 0; i <= delta; ++i) {
    577         if (equalIgnoringCase(searchCharacters + i, matchString, matchLength))
    578             return index + i;
    579     }
    580     return notFound;
     577    unsigned i = 0;
     578    // keep looping until we match
     579    while (!equalIgnoringCase(searchCharacters + i, matchString, matchLength)) {
     580        if (i == delta)
     581            return notFound;
     582        ++i;
     583    }
     584    return index + i;
    581585}
    582586
     
    615619    }
    616620
    617     for (unsigned i = 0; i <= delta; ++i) {
    618         if (searchHash == matchHash && memcmp(searchCharacters + i, matchCharacters, matchLength * sizeof(UChar)) == 0)
    619             return index + i;
     621    unsigned i = 0;
     622    // keep looping until we match
     623    while (searchHash != matchHash || memcmp(searchCharacters + i, matchCharacters, matchLength * sizeof(UChar))) {
     624        if (i == delta)
     625            return notFound;
    620626        searchHash += searchCharacters[i + matchLength];
    621627        searchHash -= searchCharacters[i];
    622     }
    623     return notFound;
     628        ++i;
     629    }
     630    return index + i;
    624631}
    625632
     
    645652    const UChar* matchCharacters = matchString->characters();
    646653
    647     for (unsigned i = 0; i <= delta; ++i) {
    648         if (equalIgnoringCase(searchCharacters + i, matchCharacters, matchLength))
    649             return index + i;
    650     }
    651     return notFound;
     654    unsigned i = 0;
     655    // keep looping until we match
     656    while (!equalIgnoringCase(searchCharacters + i, matchCharacters, matchLength)) {
     657        if (i == delta)
     658            return notFound;
     659        ++i;
     660    }
     661    return index + i;
    652662}
    653663
     
    688698    }
    689699
     700    // keep looping until we match
    690701    while (searchHash != matchHash || memcmp(searchCharacters + delta, matchCharacters, matchLength * sizeof(UChar))) {
    691         if (!delta--)
     702        if (!delta)
    692703            return notFound;
     704        delta--;
    693705        searchHash -= searchCharacters[delta + matchLength];
    694706        searchHash += searchCharacters[delta];
     
    715727    const UChar *matchCharacters = matchString->characters();
    716728
     729    // keep looping until we match
    717730    while (!equalIgnoringCase(searchCharacters + delta, matchCharacters, matchLength)) {
    718         if (!delta--)
     731        if (!delta)
    719732            return notFound;
     733        delta--;
    720734    }
    721735    return delta;
Note: See TracChangeset for help on using the changeset viewer.