Ignore:
Timestamp:
Feb 15, 2010, 12:59:54 PM (15 years ago)
Author:
[email protected]
Message:

Bug 34948 - tryMakeString should fail on error in length calculation

Reviewed by Geoff Garen.

The sum of the length of substrings could overflow.

  • runtime/UString.h:

(JSC::sumWithOverflow):
(JSC::tryMakeString):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UString.h

    r54747 r54788  
    327327    };
    328328
     329    inline void sumWithOverflow(unsigned& total, unsigned addend, bool overflow)
     330    {
     331        unsigned oldTotal = total;
     332        total = oldTotal + addend;
     333        if (total < oldTotal)
     334            overflow = true;
     335    }
     336
    329337    template<typename StringType1, typename StringType2>
    330338    PassRefPtr<UStringImpl> tryMakeString(StringType1 string1, StringType2 string2)
     
    334342
    335343        UChar* buffer;
    336         unsigned length = adapter1.length() + adapter2.length();
     344        bool overflow = false;
     345        unsigned length = adapter1.length();
     346        sumWithOverflow(length, adapter2.length(), overflow);
     347        if (overflow)
     348            return 0;
    337349        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    338350        if (!resultImpl)
     
    355367
    356368        UChar* buffer;
    357         unsigned length = adapter1.length() + adapter2.length() + adapter3.length();
     369        bool overflow = false;
     370        unsigned length = adapter1.length();
     371        sumWithOverflow(length, adapter2.length(), overflow);
     372        sumWithOverflow(length, adapter3.length(), overflow);
     373        if (overflow)
     374            return 0;
    358375        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    359376        if (!resultImpl)
     
    379396
    380397        UChar* buffer;
    381         unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length();
     398        bool overflow = false;
     399        unsigned length = adapter1.length();
     400        sumWithOverflow(length, adapter2.length(), overflow);
     401        sumWithOverflow(length, adapter3.length(), overflow);
     402        sumWithOverflow(length, adapter4.length(), overflow);
     403        if (overflow)
     404            return 0;
    382405        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    383406        if (!resultImpl)
     
    406429
    407430        UChar* buffer;
    408         unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length();
     431        bool overflow = false;
     432        unsigned length = adapter1.length();
     433        sumWithOverflow(length, adapter2.length(), overflow);
     434        sumWithOverflow(length, adapter3.length(), overflow);
     435        sumWithOverflow(length, adapter4.length(), overflow);
     436        sumWithOverflow(length, adapter5.length(), overflow);
     437        if (overflow)
     438            return 0;
    409439        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    410440        if (!resultImpl)
     
    436466
    437467        UChar* buffer;
    438         unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length();
     468        bool overflow = false;
     469        unsigned length = adapter1.length();
     470        sumWithOverflow(length, adapter2.length(), overflow);
     471        sumWithOverflow(length, adapter3.length(), overflow);
     472        sumWithOverflow(length, adapter4.length(), overflow);
     473        sumWithOverflow(length, adapter5.length(), overflow);
     474        sumWithOverflow(length, adapter6.length(), overflow);
     475        if (overflow)
     476            return 0;
    439477        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    440478        if (!resultImpl)
     
    469507
    470508        UChar* buffer;
    471         unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length() + adapter7.length();
     509        bool overflow = false;
     510        unsigned length = adapter1.length();
     511        sumWithOverflow(length, adapter2.length(), overflow);
     512        sumWithOverflow(length, adapter3.length(), overflow);
     513        sumWithOverflow(length, adapter4.length(), overflow);
     514        sumWithOverflow(length, adapter5.length(), overflow);
     515        sumWithOverflow(length, adapter6.length(), overflow);
     516        sumWithOverflow(length, adapter7.length(), overflow);
     517        if (overflow)
     518            return 0;
    472519        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    473520        if (!resultImpl)
     
    505552
    506553        UChar* buffer;
    507         unsigned length = adapter1.length() + adapter2.length() + adapter3.length() + adapter4.length() + adapter5.length() + adapter6.length() + adapter7.length() + adapter8.length();
     554        bool overflow = false;
     555        unsigned length = adapter1.length();
     556        sumWithOverflow(length, adapter2.length(), overflow);
     557        sumWithOverflow(length, adapter3.length(), overflow);
     558        sumWithOverflow(length, adapter4.length(), overflow);
     559        sumWithOverflow(length, adapter5.length(), overflow);
     560        sumWithOverflow(length, adapter6.length(), overflow);
     561        sumWithOverflow(length, adapter7.length(), overflow);
     562        sumWithOverflow(length, adapter8.length(), overflow);
     563        if (overflow)
     564            return 0;
    508565        PassRefPtr<UStringImpl> resultImpl = UStringImpl::tryCreateUninitialized(length, buffer);
    509566        if (!resultImpl)
Note: See TracChangeset for help on using the changeset viewer.