Bug 872853 - Make +"0x" evaluate to NaN. r=evilpie
authorJeff Walden <jwalden@mit.edu>
Thu, 16 May 2013 16:29:53 -0700
changeset 143771 8ed13280c8116ed1b09ff4919b0987bf9cb8e9f4
parent 143770 23741f85220a7a3dcce1c1ca65d44ae7ff2761a7
child 143772 c3b24901a5184f9b26ae4fc5da1e037b9b17b143
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevilpie
bugs872853
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 872853 - Make +"0x" evaluate to NaN. r=evilpie
js/src/jsnum.cpp
js/src/tests/ecma_5/Number/tonumber-string-hex.js
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -1352,20 +1352,27 @@ StringToNumber(JSContext *cx, JSString *
         return true;
     }
 
     const jschar *end = chars + length;
     const jschar *bp = SkipSpace(chars, end);
 
     /* 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;
         }
         *result = d;
         return true;
     }
 
     /*
new file mode 100644
--- /dev/null
+++ b/js/src/tests/ecma_5/Number/tonumber-string-hex.js
@@ -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");