Reviewers: MikeSamuel,

Description:
What steps will reproduce the problem?
1. var x= 99999999999999999999999;

What is the expected output? What do you see instead?

Caused by: java.lang.NumberFormatException: For input string:
"99999999999999999999999"
        at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Long.parseLong(Long.java:441)
        at java.lang.Long.valueOf(Long.java:508)
        at java.lang.Long.decode(Long.java:653)
        at com.google.caja.parser.js.Parser.toIntegerLiteral(Parser.java:996)
        at
com.google.caja.parser.js.Parser.parseExpressionAtom(Parser.java:1026)
        at com.google.caja.parser.js.Parser.parseOp(Parser.java:792)
        at com.google.caja.parser.js.Parser.parseOp(Parser.java:858)
        at com.google.caja.parser.js.Parser.parseExpressionInt(Parser.java:727)

This change reparses the number literal as a BigDecimal before
evaluating whether or not it can be represented

Please review this at http://codereview.appspot.com/2333044/

Affected files:
  M     src/com/google/caja/parser/js/Parser.java
  M     tests/com/google/caja/parser/js/ParserTest.java


Index: tests/com/google/caja/parser/js/ParserTest.java
===================================================================
--- tests/com/google/caja/parser/js/ParserTest.java     (revision 4291)
+++ tests/com/google/caja/parser/js/ParserTest.java     (working copy)
@@ -37,6 +37,7 @@
 import com.google.caja.util.TestUtil;

 import java.io.IOException;
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -329,6 +330,12 @@
     assertEquals(new Double(9223372036854776000d), l.getValue());
   }

+  public final void testOutOfRangeLiterals2() throws Exception {
+ NumberLiteral l = (NumberLiteral) jsExpr(fromString("99999999999999999999999"));
+    assertMessage(
+        MessageType.UNREPRESENTABLE_INTEGER_LITERAL, MessageLevel.WARNING);
+  }
+
   public final void testRedundantEscapeSequences() throws Exception {
// Should issue a warning if there is an escape sequence in a string where // the escaped character is not interpreted differently, and the escaped
Index: src/com/google/caja/parser/js/Parser.java
===================================================================
--- src/com/google/caja/parser/js/Parser.java   (revision 4291)
+++ src/com/google/caja/parser/js/Parser.java   (working copy)
@@ -979,7 +979,12 @@
   }

   private strictfp long toInteger(Token<JsTokenType> t) {
-    Long longValue = Long.decode(t.text);
+    Number longValue;
+    try {
+      longValue = Long.decode(t.text);
+    } catch (NumberFormatException e) {
+      longValue = new BigDecimal(t.text);
+    }

     // Make sure that the number fits in a 51 bit mantissa
     long lv = longValue.longValue();
@@ -993,7 +998,12 @@
   }

   private NumberLiteral toIntegerLiteral(Token<JsTokenType> t) {
-    Long longValue = Long.decode(t.text);
+    Number longValue;
+    try {
+      longValue = Long.decode(t.text);
+    } catch (NumberFormatException e) {
+      longValue = new BigDecimal(t.text);
+    }

     // Make sure that the number fits in a 51 bit mantissa
     long lv = longValue.longValue();


Reply via email to