From: markt Date: Tue, 6 Jul 2010 16:47:39 +0000 (+0000) Subject: Improve fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=49217 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=a83072fbbbd494b734320aec23798e10daa59d51;p=tomcat7.0 Improve fix for https://issues.apache.org/bugzilla/show_bug.cgi?id=49217 Make sure identifiers meet the requirements of the JLS git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@960942 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/el/parser/AstDotSuffix.java b/java/org/apache/el/parser/AstDotSuffix.java index d925c1159..5e964f1b6 100644 --- a/java/org/apache/el/parser/AstDotSuffix.java +++ b/java/org/apache/el/parser/AstDotSuffix.java @@ -41,7 +41,7 @@ public final class AstDotSuffix extends SimpleNode { @Override public void setImage(String image) { - if (Validation.isJavaKeyword(image)) { + if (!Validation.isIdentifier(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 2283a8db3..d4c477a98 100644 --- a/java/org/apache/el/parser/AstIdentifier.java +++ b/java/org/apache/el/parser/AstIdentifier.java @@ -128,7 +128,7 @@ public final class AstIdentifier extends SimpleNode { @Override public void setImage(String image) { - if (Validation.isJavaKeyword(image)) { + if (!Validation.isIdentifier(image)) { throw new ELException("Can't use Java keyword as identifier"); } this.image = image; diff --git a/java/org/apache/el/util/Validation.java b/java/org/apache/el/util/Validation.java index df2eb5b49..b291fed11 100644 --- a/java/org/apache/el/util/Validation.java +++ b/java/org/apache/el/util/Validation.java @@ -19,15 +19,16 @@ 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" }; + // Java keywords, boolean literals & the null literal in alphabetical order + private static final String invalidIdentifiers[] = { "abstract", "assert", + "boolean", "break", "byte", "case", "catch", "char", "class", "const", + "continue", "default", "do", "double", "else", "enum", "extends", + "false", "final", "finally", "float", "for", "goto", "if", "implements", + "import", "instanceof", "int", "interface", "long", "native", "new", + "null", "package", "private", "protected", "public", "return", "short", + "static", "strictfp", "super", "switch", "synchronized", "this", + "throw", "throws", "transient", "true", "try", "void", "volatile", + "while" }; private Validation() { @@ -35,16 +36,23 @@ public class Validation { } /** - * Test whether the argument is a Java keyword + * Test whether the argument is a Java identifier. */ - public static boolean isJavaKeyword(String key) { + public static boolean isIdentifier(String key) { + + // Should not be the case but check to be sure + if (key == null || key.length() == 0) { + return false; + } + + // Check the list of known invalid values int i = 0; - int j = javaKeywords.length; + int j = invalidIdentifiers.length; while (i < j) { int k = (i + j) / 2; - int result = javaKeywords[k].compareTo(key); + int result = invalidIdentifiers[k].compareTo(key); if (result == 0) { - return true; + return false; } if (result < 0) { i = k + 1; @@ -52,6 +60,19 @@ public class Validation { j = k; } } - return false; + + // Check the start character that has more restrictions + if (!Character.isJavaIdentifierStart(key.charAt(0))) { + return false; + } + + // Check each remaining character used is permitted + for (int idx = 1; idx < key.length(); idx++) { + if (!Character.isJavaIdentifierPart(key.charAt(idx))) { + return false; + } + } + + return true; } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 819b7c839..9b30ea272 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -128,7 +128,8 @@ MethodExpressions is used in EL. (markt) - 49217: Prevent use of Java keywords in identifiers. (markt) + 49217: Ensure that identifiers used in EL meet the + requirements of the Java Language Specification. (markt)