From bc8473f7c6fa2cae09f0ac1f12f1931c30c03bae Mon Sep 17 00:00:00 2001 From: markt Date: Mon, 5 Jul 2010 21:45:26 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49217 Prevent use of Java keywords in identifiers git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@960712 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/el/parser/AstDotSuffix.java | 9 +++++ java/org/apache/el/parser/AstIdentifier.java | 9 +++++ java/org/apache/el/util/Validation.java | 57 ++++++++++++++++++++++++++++ webapps/docs/changelog.xml | 3 ++ 4 files changed, 78 insertions(+) create mode 100644 java/org/apache/el/util/Validation.java diff --git a/java/org/apache/el/parser/AstDotSuffix.java b/java/org/apache/el/parser/AstDotSuffix.java index 009465c99..d925c1159 100644 --- a/java/org/apache/el/parser/AstDotSuffix.java +++ b/java/org/apache/el/parser/AstDotSuffix.java @@ -21,6 +21,7 @@ package org.apache.el.parser; import javax.el.ELException; import org.apache.el.lang.EvaluationContext; +import org.apache.el.util.Validation; /** @@ -37,4 +38,12 @@ public final class AstDotSuffix extends SimpleNode { throws ELException { return this.image; } + + @Override + public void setImage(String image) { + if (Validation.isJavaKeyword(image)) { + throw new ELException("Can't use Java keyword as identifier"); + } + this.image = image; + } } diff --git a/java/org/apache/el/parser/AstIdentifier.java b/java/org/apache/el/parser/AstIdentifier.java index 6bb894611..2283a8db3 100644 --- a/java/org/apache/el/parser/AstIdentifier.java +++ b/java/org/apache/el/parser/AstIdentifier.java @@ -28,6 +28,7 @@ import javax.el.VariableMapper; import org.apache.el.lang.EvaluationContext; import org.apache.el.util.MessageFactory; +import org.apache.el.util.Validation; /** @@ -125,6 +126,14 @@ public final class AstIdentifier extends SimpleNode { return this.getMethodExpression(ctx).getMethodInfo(ctx.getELContext()); } + @Override + public void setImage(String image) { + if (Validation.isJavaKeyword(image)) { + throw new ELException("Can't use Java keyword as identifier"); + } + this.image = image; + } + private final MethodExpression getMethodExpression(EvaluationContext ctx) throws ELException { Object obj = null; diff --git a/java/org/apache/el/util/Validation.java b/java/org/apache/el/util/Validation.java new file mode 100644 index 000000000..df2eb5b49 --- /dev/null +++ b/java/org/apache/el/util/Validation.java @@ -0,0 +1,57 @@ +/* + * 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. + */ + +package org.apache.el.util; + +public class Validation { + + private static final String javaKeywords[] = { "abstract", "assert", + "boolean", "break", "byte", "case", "catch", "char", "class", + "const", "continue", "default", "do", "double", "else", "enum", + "extends", "final", "finally", "float", "for", "goto", "if", + "implements", "import", "instanceof", "int", "interface", "long", + "native", "new", "package", "private", "protected", "public", + "return", "short", "static", "strictfp", "super", "switch", + "synchronized", "this", "throw", "throws", "transient", "try", + "void", "volatile", "while" }; + + + private Validation() { + // Utility class. Hide default constructor + } + + /** + * Test whether the argument is a Java keyword + */ + public static boolean isJavaKeyword(String key) { + int i = 0; + int j = javaKeywords.length; + while (i < j) { + int k = (i + j) / 2; + int result = javaKeywords[k].compareTo(key); + if (result == 0) { + return true; + } + if (result < 0) { + i = k + 1; + } else { + j = k; + } + } + return false; + } +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index aba3c9ab8..819b7c839 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -127,6 +127,9 @@ Correct algorithm used to identify correct method to use when a MethodExpressions is used in EL. (markt) + + 49217: Prevent use of Java keywords in identifiers. (markt) + -- 2.11.0