8354908: javac mishandles supplementary character in character literal

Reviewed-by: naoto, vromero
This commit is contained in:
Jan Lahoda 2025-05-13 06:16:34 +00:00
parent 6e0846918c
commit 03dca0323d
4 changed files with 62 additions and 2 deletions

View File

@ -389,6 +389,10 @@ public class JavaTokenizer extends UnicodeReader {
break;
}
} else {
if (!isString && !Character.isBmpCodePoint(getCodepoint())) {
lexError(pos, Errors.IllegalCharLiteralMultipleSurrogates);
}
putThenNext();
}
}

View File

@ -694,6 +694,9 @@ compiler.err.illegal.combination.of.modifiers=\
compiler.err.illegal.enum.static.ref=\
illegal reference to static field from initializer
compiler.err.illegal.char.literal.multiple.surrogates=\
character literal contains more than one UTF-16 code unit
compiler.err.illegal.esc.char=\
illegal escape character

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.illegal.char.literal.multiple.surrogates
class IllegalCharLiteralMultipleSurrogates {
char c = '\uD83D\uDE0A';
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,8 +30,11 @@
*/
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.tools.DiagnosticListener;
import javax.tools.SimpleJavaFileObject;
import com.sun.tools.javac.parser.JavaTokenizer;
@ -101,17 +104,29 @@ public class JavaLexerTest {
new TestTuple(ERROR, "\'\'"),
new TestTuple(ERROR, "\'\\q\'", "\'\\q\'"),
new TestTuple(ERROR, "\'\\{1+2}\'", "\'\\{1+2}\'"),
new TestTuple(ERROR, "'\uD83D\uDE0A'",
List.of("compiler.err.illegal.char.literal.multiple.surrogates")),
};
static class TestTuple {
String input;
TokenKind kind;
String expected;
List<String> expectedErrors;
TestTuple(TokenKind kind, String input, String expected) {
TestTuple(TokenKind kind, String input, String expected, List<String> expectedErrors) {
this.input = input;
this.kind = kind;
this.expected = expected;
this.expectedErrors = expectedErrors;
}
TestTuple(TokenKind kind, String input, List<String> expectedErrors) {
this(kind, input, input, expectedErrors);
}
TestTuple(TokenKind kind, String input, String expected) {
this(kind, input, expected, null);
}
TestTuple(TokenKind kind, String input) {
@ -121,6 +136,12 @@ public class JavaLexerTest {
void test(TestTuple test, boolean willFail) throws Exception {
Context ctx = new Context();
List<String> errors = new ArrayList();
if (test.expectedErrors != null) {
ctx.put(DiagnosticListener.class, (DiagnosticListener) d -> errors.add(d.getCode()));
}
Log log = Log.instance(ctx);
log.useSource(SimpleJavaFileObject.forSource(URI.create("mem://Test.java"),
@ -149,6 +170,10 @@ public class JavaLexerTest {
System.err.println("input: " + test.input);
throw new AssertionError("Unexpected token content: " + actual);
}
if (test.expectedErrors != null && !test.expectedErrors.equals(errors)) {
throw new AssertionError("Unexpected errors: " + errors);
}
}
void run() throws Exception {