Ignore:
Timestamp:
Jul 17, 2007, 7:25:38 PM (18 years ago)
Author:
darin
Message:

JavaScriptCore:

Reviewed by Darin, Maciej, and Adam.

Fixes <http://bugs.webkit.org/show_bug.cgi?id=9697>,

the failure of ecma/GlobalObject/15.1.2.2-2.js,
the failure of ecma/LexicalConventions/7.7.3-1.js,
and most of the failures of tests in ecma/TypeConversion/9.3.1-3.js.

Bug 9697: parseInt results may be inaccurate for numbers greater than 253

This patch also fixes similar issues in the lexer and UString::toDouble().

  • kjs/function.cpp: (KJS::parseIntOverflow): (KJS::parseInt):
  • kjs/function.h:
  • kjs/lexer.cpp: (KJS::Lexer::lex):
  • kjs/ustring.cpp: (KJS::UString::toDouble):
  • tests/mozilla/expected.html:

LayoutTests:

Reviewed by Darin.

Added tests for:
http://bugs.webkit.org/show_bug.cgi?id=9697

Bug 9697: parseInt results may be inaccurate for numbers greater than 253

  • fast/js/numeric-conversion-expected.txt: Added.
  • fast/js/numeric-conversion.html: Added.
  • fast/js/resources/numeric-conversion.js: Added.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/function.cpp

    r24244 r24394  
    55 *  Copyright (C) 2001 Peter Kelly ([email protected])
    66 *  Copyright (C) 2003 Apple Computer, Inc.
     7 *  Copyright (C) 2007 Cameron Zwarich ([email protected])
    78 *
    89 *  This library is free software; you can redistribute it and/or
     
    2627#include "function.h"
    2728
     29#include "dtoa.h"
    2830#include "internal.h"
    2931#include "function_object.h"
     
    670672}
    671673
     674double parseIntOverflow(const char* s, int length, int radix)
     675{
     676    double number = 0.0;
     677    double radixMultiplier = 1.0;
     678
     679    for (const char* p = s + length - 1; p >= s; p--) {
     680        if (radixMultiplier == Inf) {
     681            if (*p != '0') {
     682                number = Inf;
     683                break;
     684            }
     685        } else {
     686            int digit = parseDigit(*p, radix);
     687            number += digit * radixMultiplier;
     688        }
     689
     690        radixMultiplier *= radix;
     691    }
     692
     693    return number;
     694}
     695
    672696static double parseInt(const UString& s, int radix)
    673697{
     
    702726        return NaN;
    703727
     728    int firstDigitPosition = p;
    704729    bool sawDigit = false;
    705730    double number = 0;
     
    712737        number += digit;
    713738        ++p;
     739    }
     740
     741    if (number >= mantissaOverflowLowerBound) {
     742        if (radix == 10)
     743            number = kjs_strtod(s.substr(firstDigitPosition, p - firstDigitPosition).ascii(), 0);
     744        else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32)
     745            number = parseIntOverflow(s.substr(firstDigitPosition, p - firstDigitPosition).ascii(), p - firstDigitPosition, radix);
    714746    }
    715747
Note: See TracChangeset for help on using the changeset viewer.