# HG changeset patch # User jlahoda # Date 1563266902 -7200 # Node ID 1bb102c1cf276961a2563382e732f06b0f420db1 # Parent 04d80e7ad3f4be8f613f3d8f6d78fc1e12f255b3 8227640: javac crashes on text blocks with invalid escapes. Summary: Do not translate escape sequences if text block contains illegal escapes. Reviewed-by: jlaskey diff -r 04d80e7ad3f4 -r 1bb102c1cf27 src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java --- 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. diff -r 04d80e7ad3f4 -r 1bb102c1cf27 test/langtools/tools/javac/TextBlockIllegalEscape.java --- /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(""" + \! + """, ""); + } +} diff -r 04d80e7ad3f4 -r 1bb102c1cf27 test/langtools/tools/javac/TextBlockIllegalEscape.out --- /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