Bug 872853 - Make +"0x" evaluate to NaN. r=evilpie

--HG--
extra : rebase_source : ec46e8224922729a7f8ec1bb023f4e6733106c7c
This commit is contained in:
Jeff Walden 2013-05-16 16:29:53 -07:00
parent f9c7c15a05
commit 8f6a835c0c
2 changed files with 47 additions and 2 deletions

View File

@ -1357,10 +1357,17 @@ StringToNumber(JSContext *cx, JSString *str, double *result)
/* ECMA doesn't allow signed hex numbers (bug 273467). */
if (end - bp >= 2 && bp[0] == '0' && (bp[1] == 'x' || bp[1] == 'X')) {
/* Looks like a hex number. */
/*
* It's probably a hex number. Accept if there's at least one hex
* digit after the 0x, and if no non-whitespace characters follow all
* the hex digits.
*/
const jschar *endptr;
double d;
if (!GetPrefixInteger(cx, bp + 2, end, 16, &endptr, &d) || SkipSpace(endptr, end) != end) {
if (!GetPrefixInteger(cx, bp + 2, end, 16, &endptr, &d) ||
endptr == bp + 2 ||
SkipSpace(endptr, end) != end)
{
*result = js_NaN;
return true;
}

View File

@ -0,0 +1,38 @@
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommonn.org/licenses/publicdomain/
*/
var BUGNUMBER = 872853;
var summary = 'Various tests of ToNumber(string), particularly +"0x" being NaN';
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
assertEq(+"0x", NaN);
assertEq(+"\t0x", NaN);
assertEq(+"0x\n", NaN);
assertEq(+"\n0x\t", NaN);
assertEq(+"0x0", 0);
assertEq(+"0xa", 10);
assertEq(+"0xff", 255);
assertEq(+"-0x", NaN);
assertEq(+"-0xa", NaN);
assertEq(+"-0xff", NaN);
assertEq(+"0xInfinity", NaN);
assertEq(+"+Infinity", Infinity);
assertEq(+"-Infinity", -Infinity);
assertEq(+"\t+Infinity", Infinity);
assertEq(+"-Infinity\n", -Infinity);
assertEq(+"+ Infinity", NaN);
assertEq(+"- Infinity", NaN);
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");