8227640: javac crashes on text blocks with invalid escapes

Do not translate escape sequences if text block contains illegal escapes.

Reviewed-by: jlaskey
This commit is contained in:
Jan Lahoda 2019-07-16 10:48:22 +02:00
parent 768ef3cd20
commit c0d870559f
3 changed files with 25 additions and 1 deletions

View File

@ -99,6 +99,10 @@ public class JavaTokenizer {
*/
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 @@ public class JavaTokenizer {
case '\\':
reader.putChar(true); break;
default:
hasBrokenEscapes = true;
lexError(reader.bp, Errors.IllegalEscChar);
}
}
@ -426,6 +431,7 @@ public class JavaTokenizer {
// 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 @@ public class JavaTokenizer {
string = TextBlockSupport.stripIndent(string);
}
// Translate escape sequences if present.
if (shouldTranslateEscapes) {
if (shouldTranslateEscapes && !hasBrokenEscapes) {
string = TextBlockSupport.translateEscapes(string);
}
// Build string token.

View File

@ -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("""
\!
""", "");
}
}

View File

@ -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