From 2b78c1fe583feb60591e8ad12ec2eec9e8d1c65a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Thu, 26 Sep 2024 12:24:02 -0700 Subject: [PATCH] Avoid a possible `IndexOutOfBoundsException` in `MemberName`. Ordinarily we shouldn't see an empty class name but it appears that sometimes we do: [] ``` Sep 26, 2024 10:29:02 AM com.google.devtools.staticanalysis.pipeline.java.errorprone.ErrorProneFindingsJavacAnalyzer$1 handleError WARNING: crash in Error Prone analyzer MemberName [CONTEXT ratelimit_period="5 SECONDS" analyzer="JavaStyle" ] java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4881) at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46) at java.base/java.lang.String.charAt(String.java:1582) at com.google.errorprone.bugpatterns.MemberName.isConformantUpperCamelName(MemberName.java:290) at com.google.errorprone.bugpatterns.MemberName.matchClass(MemberName.java:111) at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:460) at com.google.errorprone.scanner.ErrorProneScanner.visitClass(ErrorProneScanner.java:558) at com.google.errorprone.scanner.ErrorProneScanner.visitClass(ErrorProneScanner.java:157) ``` PiperOrigin-RevId: 679249176 --- .../java/com/google/errorprone/bugpatterns/MemberName.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java index 2a161c419f0..19e60c1734c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java @@ -101,7 +101,8 @@ public final class MemberName extends BugChecker public Description matchClass(ClassTree tree, VisitorState state) { ClassSymbol symbol = getSymbol(tree); String name = tree.getSimpleName().toString(); - if (isConformantUpperCamelName(name)) { + if (name.isEmpty() || isConformantUpperCamelName(name)) { + // name.isEmpty() should not happen normally but could if there are errors. return NO_MATCH; } String renamed = suggestedClassRename(name);