⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 167548 in webkit


Ignore:
Timestamp:
Apr 19, 2014, 2:13:46 PM (12 years ago)
Author:
fpizlo@apple.com
Message:

Make it easier to check if an integer sum would overflow
https://bugs.webkit.org/show_bug.cgi?id=131900

Reviewed by Darin Adler.

Source/JavaScriptCore:

  • dfg/DFGOperations.cpp:
  • runtime/Operations.h:

(JSC::jsString):

Source/WTF:

  • wtf/CheckedArithmetic.h:

(WTF::checkedSum):
(WTF::sumOverflows):

Location:
trunk/Source
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r167544 r167548  
     12014-04-19  Filip Pizlo  <fpizlo@apple.com>
     2
     3        Make it easier to check if an integer sum would overflow
     4        https://bugs.webkit.org/show_bug.cgi?id=131900
     5
     6        Reviewed by Darin Adler.
     7
     8        * dfg/DFGOperations.cpp:
     9        * runtime/Operations.h:
     10        (JSC::jsString):
     11
    1122014-04-19  Filip Pizlo  <fpizlo@apple.com>
    213
  • trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp

    r167544 r167548  
    969969    VM& vm = exec->vm();
    970970    NativeCallFrameTracer tracer(&vm, exec);
    971    
    972     if (static_cast<int32_t>(left->length() + right->length()) < 0) {
     971
     972    if (sumOverflows<int32_t>(left->length(), right->length())) {
    973973        throwOutOfMemoryError(exec);
    974974        return nullptr;
     
    983983    NativeCallFrameTracer tracer(&vm, exec);
    984984
    985     Checked<int32_t, RecordOverflow> length = a->length();
    986     length += b->length();
    987     length += c->length();
    988     if (length.hasOverflowed()) {
     985    if (sumOverflows<int32_t>(a->length(), b->length(), c->length())) {
    989986        throwOutOfMemoryError(exec);
    990987        return nullptr;
  • trunk/Source/JavaScriptCore/runtime/Operations.h

    r167336 r167548  
    4545    if (!length2)
    4646        return s1;
    47     if ((length1 + length2) < 0)
     47    if (sumOverflows<int32_t>(length1, length2))
    4848        return throwOutOfMemoryError(exec);
    4949
     
    6969        return jsString(exec, jsString(vm, u1), jsString(vm, u2));
    7070
    71     if ((length1 + length2) < 0)
    72         return throwOutOfMemoryError(exec);
    73     if ((length1 + length2 + length3) < 0)
     71    if (sumOverflows<int32_t>(length1, length2, length3))
    7472        return throwOutOfMemoryError(exec);
    7573
  • trunk/Source/WTF/ChangeLog

    r167528 r167548  
     12014-04-19  Filip Pizlo  <fpizlo@apple.com>
     2
     3        Make it easier to check if an integer sum would overflow
     4        https://bugs.webkit.org/show_bug.cgi?id=131900
     5
     6        Reviewed by Darin Adler.
     7
     8        * wtf/CheckedArithmetic.h:
     9        (WTF::checkedSum):
     10        (WTF::sumOverflows):
     11
    1122014-04-18  Commit Queue  <commit-queue@webkit.org>
    213
  • trunk/Source/WTF/wtf/CheckedArithmetic.h

    r160687 r167548  
    717717typedef Checked<size_t, RecordOverflow> CheckedSize;
    718718
     719template<typename T, typename U>
     720Checked<T, RecordOverflow> checkedSum(U value)
     721{
     722    return Checked<T, RecordOverflow>(value);
     723}
     724template<typename T, typename U, typename... Args>
     725Checked<T, RecordOverflow> checkedSum(U value, Args... args)
     726{
     727    return Checked<T, RecordOverflow>(value) + checkedSum<T>(args...);
     728}
     729
     730// Sometimes, you just want to check if some math would overflow - the code to do the math is
     731// already in place, and you want to guard it.
     732
     733template<typename T, typename... Args> bool sumOverflows(Args... args)
     734{
     735    return checkedSum<T>(args...).hasOverflowed();
     736}
     737
    719738}
    720739
     
    731750using WTF::CheckedUint64;
    732751using WTF::CheckedSize;
     752using WTF::checkedSum;
     753using WTF::sumOverflows;
    733754
    734755#endif
Note: See TracChangeset for help on using the changeset viewer.