Skip to content

Commit

Permalink
In UnicodeInCode, allow ASCII_SUB as the last character in the bu…
Browse files Browse the repository at this point in the history
…ffer

#3092

PiperOrigin-RevId: 467666418
  • Loading branch information
cushon authored and Error Prone Team committed Aug 15, 2022
1 parent 3215a56 commit c3d267e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Expand Up @@ -51,9 +51,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
String sourceCode = state.getSourceCode().toString();

for (int i = 0; i < sourceCode.length(); ++i) {
char c = sourceCode.charAt(i);

if (!isAcceptableAscii(c)) {
if (!isAcceptableAscii(sourceCode, i)) {
violations.add(Range.closedOpen(i, i + 1));
}
}
Expand Down Expand Up @@ -83,6 +81,19 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
return NO_MATCH;
}

private static boolean isAcceptableAscii(String sourceCode, int i) {
char c = sourceCode.charAt(i);
if (isAcceptableAscii(c)) {
return true;
}
if (c == 0x1a && i == sourceCode.length() - 1) {
// javac inserts ASCII_SUB characters at the end of the input, see:
// https://github.com/google/error-prone/issues/3092
return true;
}
return false;
}

private static boolean isAcceptableAscii(char c) {
return (c >= 0x20 && c <= 0x7E) || c == '\n' || c == '\r' || c == '\t';
}
Expand Down
Expand Up @@ -16,7 +16,21 @@

package com.google.errorprone.bugpatterns;

import static com.google.common.truth.Truth.assertWithMessage;
import static java.util.Locale.ENGLISH;
import static java.util.stream.Collectors.joining;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.FileManagers;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.file.JavacFileManager;
import java.net.URI;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -121,4 +135,34 @@ public void suppressibleAtMethodLevel() {
"}")
.doTest();
}

@Test
public void asciiSub() {
JavacFileManager fileManager = FileManagers.testFileManager();
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
JavacTask task =
JavacTool.create()
.getTask(
null,
fileManager,
diagnosticCollector,
ImmutableList.of("-Xplugin:ErrorProne", "-XDcompilePolicy=simple"),
ImmutableList.of(),
ImmutableList.of(
new SimpleJavaFileObject(
URI.create("file:///Test.java"), JavaFileObject.Kind.SOURCE) {
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
return "class Test {}" + ((char) 0x1a);
}
}));
boolean ok = task.call();
assertWithMessage(
diagnosticCollector.getDiagnostics().stream()
.filter(d -> d.getKind() == Diagnostic.Kind.ERROR)
.map(d -> d.getMessage(ENGLISH))
.collect(joining("\n")))
.that(ok)
.isTrue();
}
}

0 comments on commit c3d267e

Please sign in to comment.