8227640: javac crashes on text blocks with invalid escapes.
Summary: Do not translate escape sequences if text block contains illegal escapes.
Reviewed-by: jlaskey
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java Mon Jul 15 16:25:23 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java Tue Jul 16 10:48:22 2019 +0200
@@ -99,6 +99,10 @@
*/
protected boolean shouldTranslateEscapes;
+ /** Has the string broken escapes?
+ */
+ protected boolean hasBrokenEscapes;
+
protected ScannerFactory fac;
// The set of lint options currently in effect. It is initialized
@@ -261,6 +265,7 @@
case '\\':
reader.putChar(true); break;
default:
+ hasBrokenEscapes = true;
lexError(reader.bp, Errors.IllegalEscChar);
}
}
@@ -426,6 +431,7 @@
// Clear flags.
shouldStripIndent = false;
shouldTranslateEscapes = false;
+ hasBrokenEscapes = false;
// Check if text block string methods are present.
boolean hasTextBlockSupport = TextBlockSupport.hasSupport();
// Track the end of first line for error recovery.
@@ -1038,7 +1044,7 @@
string = TextBlockSupport.stripIndent(string);
}
// Translate escape sequences if present.
- if (shouldTranslateEscapes) {
+ if (shouldTranslateEscapes && !hasBrokenEscapes) {
string = TextBlockSupport.translateEscapes(string);
}
// Build string token.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/TextBlockIllegalEscape.java Tue Jul 16 10:48:22 2019 +0200
@@ -0,0 +1,14 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8227640
+ * @summary Verify that illegal escapes in text blocks do not crash the javac.
+ * @compile/fail/ref=TextBlockIllegalEscape.out --enable-preview -source ${jdk.version} -XDrawDiagnostics TextBlockIllegalEscape.java
+ */
+
+public class TextBlockIllegalEscape {
+ static void test() {
+ EQ("""
+ \!
+ """, "");
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/TextBlockIllegalEscape.out Tue Jul 16 10:48:22 2019 +0200
@@ -0,0 +1,4 @@
+TextBlockIllegalEscape.java:11:13: compiler.err.illegal.esc.char
+- compiler.note.preview.filename: TextBlockIllegalEscape.java
+- compiler.note.preview.recompile
+1 error