You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/08/06 15:04:51 UTC

groovy git commit: Validate number format

Repository: groovy
Updated Branches:
  refs/heads/master cc91ef3e0 -> 1edfb974d


Validate number format


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/1edfb974
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/1edfb974
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/1edfb974

Branch: refs/heads/master
Commit: 1edfb974dec232be1000629943d3437e7ce2f214
Parents: cc91ef3
Author: sunlan <su...@apache.org>
Authored: Sun Aug 6 23:04:36 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sun Aug 6 23:04:36 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 45 ++++++++++++++++----
 .../parser/antlr4/GroovyParserTest.groovy       |  4 ++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++
 .../src/test/resources/core/Number_01x.groovy   | 24 +++++++++++
 .../src/test/resources/fail/Number_01x.groovy   | 19 +++++++++
 .../src/test/resources/fail/Number_02x.groovy   | 19 +++++++++
 6 files changed, 107 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index d3773f6..f980110 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -255,6 +255,10 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
         this.configureScriptClassNode();
 
+        if (null != this.numberFormatError) {
+            throw createParsingFailedException(this.numberFormatError.value.getMessage(), this.numberFormatError.key);
+        }
+
         return moduleNode;
     }
 
@@ -2495,16 +2499,22 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             }
             case SUB: {
                 if (expression instanceof ConstantExpression && !insidePar) {
+                    this.numberFormatError = null; // reset the numberFormatError, try to parse the negative number
+
                     ConstantExpression constantExpression = (ConstantExpression) expression;
 
-                    String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
-                    if (null != integerLiteralText) {
-                        return this.configureAST(new ConstantExpression(Numbers.parseInteger(null, SUB_STR + integerLiteralText)), ctx);
-                    }
+                    try {
+                        String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
+                        if (null != integerLiteralText) {
+                            return this.configureAST(new ConstantExpression(Numbers.parseInteger(null, SUB_STR + integerLiteralText)), ctx);
+                        }
 
-                    String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
-                    if (null != floatingPointLiteralText) {
-                        return this.configureAST(new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText)), ctx);
+                        String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
+                        if (null != floatingPointLiteralText) {
+                            return this.configureAST(new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText)), ctx);
+                        }
+                    } catch (Exception e) {
+                        throw createParsingFailedException(e.getMessage(), ctx);
                     }
 
                     throw new GroovyBugError("Failed to find the original number literal text: " + constantExpression.getText());
@@ -3089,7 +3099,14 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     public ConstantExpression visitIntegerLiteralAlt(IntegerLiteralAltContext ctx) {
         String text = ctx.IntegerLiteral().getText();
 
-        ConstantExpression constantExpression = new ConstantExpression(Numbers.parseInteger(null, text), !text.startsWith(SUB_STR));
+        Number num = null;
+        try {
+            num = Numbers.parseInteger(null, text);
+        } catch (Exception e) {
+            this.numberFormatError = new Pair<>(ctx, e);
+        }
+
+        ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
         constantExpression.putNodeMetaData(IS_NUMERIC, true);
         constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text);
 
@@ -3100,7 +3117,14 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     public ConstantExpression visitFloatingPointLiteralAlt(FloatingPointLiteralAltContext ctx) {
         String text = ctx.FloatingPointLiteral().getText();
 
-        ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal(text), !text.startsWith(SUB_STR));
+        Number num = null;
+        try {
+            num = Numbers.parseDecimal(text);
+        } catch (Exception e) {
+            this.numberFormatError = new Pair<>(ctx, e);
+        }
+
+        ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
         constantExpression.putNodeMetaData(IS_NUMERIC, true);
         constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text);
 
@@ -4506,6 +4530,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     private final Deque<ClassNode> classNodeStack = new ArrayDeque<>();
     private final Deque<List<InnerClassNode>> anonymousInnerClassesDefinedInMethodStack = new ArrayDeque<>();
     private int anonymousInnerClassCounter = 1;
+
+    private Pair<GroovyParserRuleContext, Exception> numberFormatError;
+
     private static final String QUESTION_STR = "?";
     private static final String DOT_STR = ".";
     private static final String SUB_STR = "-";

http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index cd0d935..3410dd7 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -361,6 +361,10 @@ class GroovyParserTest extends GroovyTestCase {
         doRunAndTest('core/FieldDeclaration_01x.groovy');
     }
 
+    void "test groovy core - Number"() {
+        doRunAndTest('core/Number_01x.groovy');
+    }
+
     void "test groovy core - BUG"() {
         doRunAndTest('bugs/BUG-GROOVY-4757.groovy');
         doRunAndTest('bugs/BUG-GROOVY-5652.groovy');

http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index b031654..f29c1e1 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -197,6 +197,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/VarArgParameter_01x.groovy');
     }
 
+    void "test groovy core - Number"() {
+        TestUtils.doRunAndShouldFail('fail/Number_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/Number_02x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy b/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
new file mode 100644
index 0000000..a63eb87
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
@@ -0,0 +1,24 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+def a = 2147483647I
+def b = -2147483648I
+def c = -2147483647I
+def d = 9223372036854775807L
+def e = -9223372036854775808L
+def f = -9223372036854775807L

http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
new file mode 100644
index 0000000..8559ec4
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+2147483648I

http://git-wip-us.apache.org/repos/asf/groovy/blob/1edfb974/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
new file mode 100644
index 0000000..4d7aa9d
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+9223372036854775808L