Reviewers: rossberg,

Description:
Interpret negative hexadecimal literals as NaN.


[email protected]
BUG=v8:2240
TEST=str-to-num.js


Please review this at https://chromiumcodereview.appspot.com/10818003/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/conversions-inl.h
  M test/mjsunit/str-to-num.js


Index: src/conversions-inl.h
diff --git a/src/conversions-inl.h b/src/conversions-inl.h
index 77b260f036483962892a88ec04f46624fca30aec..e8fba28f89145725ef56cd5a985b4894427afcea 100644
--- a/src/conversions-inl.h
+++ b/src/conversions-inl.h
@@ -458,6 +458,34 @@ double InternalStringToDouble(UnicodeCache* unicode_cache,
   int significant_digits = 0;
   int insignificant_digits = 0;
   bool nonzero_digit_dropped = false;
+  bool leading_zero = false;
+
+  if (*current == '0') {
+    ++current;
+    if (current == end) return SignedZero(false);
+
+    leading_zero = true;
+
+    // It could be hexadecimal value.
+    if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
+      ++current;
+      if (current == end || !isDigit(*current, 16)) {
+        return JunkStringValue();  // "0x".
+      }
+
+      return InternalStringToIntDouble<4>(unicode_cache,
+                                          current,
+                                          end,
+                                          false,
+                                          allow_trailing_junk);
+    }
+
+    // Ignore leading zeros in the integer part.
+    while (*current == '0') {
+      ++current;
+      if (current == end) return SignedZero(false);
+    }
+  }

   bool negative = false;

@@ -486,34 +514,6 @@ double InternalStringToDouble(UnicodeCache* unicode_cache,
     return negative ? -V8_INFINITY : V8_INFINITY;
   }

-  bool leading_zero = false;
-  if (*current == '0') {
-    ++current;
-    if (current == end) return SignedZero(negative);
-
-    leading_zero = true;
-
-    // It could be hexadecimal value.
-    if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) {
-      ++current;
-      if (current == end || !isDigit(*current, 16)) {
-        return JunkStringValue();  // "0x".
-      }
-
-      return InternalStringToIntDouble<4>(unicode_cache,
-                                          current,
-                                          end,
-                                          negative,
-                                          allow_trailing_junk);
-    }
-
-    // Ignore leading zeros in the integer part.
-    while (*current == '0') {
-      ++current;
-      if (current == end) return SignedZero(negative);
-    }
-  }
-
   bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0;

   // Copy significant digits of the integer part (if any) to the buffer.
Index: test/mjsunit/str-to-num.js
diff --git a/test/mjsunit/str-to-num.js b/test/mjsunit/str-to-num.js
index bbfa7d33a02b9d0e84834c7e3ed27b6694d9931c..cbec87fab93f0423d6c6975e6806eea92d35d9a0 100644
--- a/test/mjsunit/str-to-num.js
+++ b/test/mjsunit/str-to-num.js
@@ -147,7 +147,6 @@ assertEquals(15, toNumber("0Xf"));
 assertEquals(15, toNumber("0XF"));

 assertEquals(0,  toNumber("0x000"));
-assertEquals(-Infinity,  1 / toNumber("-0x000"));
 assertEquals(0,  toNumber("0x000" + repeat('0', 1000)));
 assertEquals(9,  toNumber("0x009"));
 assertEquals(10, toNumber("0x00a"));
@@ -157,7 +156,6 @@ assertEquals(15, toNumber("0x00F"));
 assertEquals(15, toNumber("0x00F "));
 assertEquals(Infinity,  toNumber("0x" + repeat('0', 1000) + '1'
                         + repeat('0', 1000)));
-assertEquals(-Infinity,  toNumber("-0x1" + repeat('0', 1000)));

 assertEquals(0x1000000 * 0x10000000, toNumber("0x10000000000000"));
 assertEquals(0x1000000 * 0x10000000 + 1, toNumber("0x10000000000001"));
@@ -207,3 +205,10 @@ assertTrue(isNaN(toNumber("1" + repeat('0', 1000) + 'junk')), "1e1000 junk");
 for (var i = 1; i < 12; i++) {
   assertEquals(toNumber('1' + repeat('0', i)), Math.pow(10.0, i));
 }
+
+assertTrue(isNaN(toNumber("+0x0")));
+assertTrue(isNaN(toNumber("+0xFF")));
+assertTrue(isNaN(toNumber("+0x012")));
+assertTrue(isNaN(toNumber("-0x0")));
+assertTrue(isNaN(toNumber("-0xFF")));
+assertTrue(isNaN(toNumber("-0x012")));
\ No newline at end of file


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to