Skip to content

Add some sanity checks around source offsets in ParameterName check - #1135

Closed
msridhar wants to merge 1 commit into
google:masterfrom
msridhar:ms/parametername-sanity-check
Closed

msridhar wants to merge 1 commit into
google:masterfrom
msridhar:ms/parametername-sanity-check

Conversation

@msridhar

@msridhar msridhar commented Oct 3, 2018

Copy link
Copy Markdown
Contributor

We've seen a couple of cases where the ParameterName check crashes on method calls that have been inserted into the AST by another tool, e.g., a Javac plugin or Lombok. These additional sanity checks seem to get rid of the crash.

Not obvious how to write a test for this, but open to suggestions.

Fixes #780

@msridhar

Copy link
Copy Markdown
Contributor Author

@cushon @eaftan ping on this. I think it fixes one of the most duplicated GitHub issues so thought you might be interested 😄

@eaftan eaftan self-assigned this Oct 10, 2018
@eaftan

eaftan commented Oct 10, 2018

Copy link
Copy Markdown
Contributor

Thanks for the contribution, this looks fine to me. I will pull it in as an internal commit, then it should get pushed back out to Github tomorrow.

@cushon

cushon commented Oct 10, 2018

Copy link
Copy Markdown
Collaborator

How complete a fix is this? Adding synthetic AST nodes isn't a supported part of the plugin API. Is ParameterName the only check it causes problems for, or is it just the first one that crashes?

@msridhar

Copy link
Copy Markdown
Contributor Author

Not sure how complete it is. I think the key thing the ParameterName check does is call getSourceCode() and then try to extract a range of characters from it. A search shows a few other checks possibly doing something similar. I think ParameterName specifically might be tripping up because tools that do this kind of (unsupported) manipulation often insert method calls. I'm open to looking at other checks more carefully to see if they could use guards as well.

cushon pushed a commit to cushon/error-prone that referenced this pull request Oct 18, 2018
@pkwarren

pkwarren commented Oct 26, 2018 •

Copy link
Copy Markdown

With this fix and the following update to FallThrough, we're able to compile some large projects which are using Lombok with the error-prone compiler (without any exclusions):

diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java b/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java
index 843756e42..1bf1d8115 100644
--- a/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java
+++ b/core/src/main/java/com/google/errorprone/bugpatterns/FallThrough.java
@@ -63,10 +63,17 @@ public class FallThrough extends BugChecker implements SwitchTreeMatcher {
       // reported an error if that statement wasn't reachable, and the answer is
       // independent of any preceding statements.
       boolean completes = Reachability.canCompleteNormally(getLast(caseTree.stats));
+      int caseEndPositionIndex = caseEndPosition(state, caseTree);
+      int nextStartPositionIndex = next.getStartPosition();
+      // check for obviously invalid locations, which can arise, e.g., when a Javac plugin does AST
+      // mutation but doesn't set source locations
+      if (caseEndPositionIndex < 0 || nextStartPositionIndex < 0 || nextStartPositionIndex <= caseEndPositionIndex) {
+        continue;
+      }
       String comments =
           state
               .getSourceCode()
-              .subSequence(caseEndPosition(state, caseTree), next.getStartPosition())
+              .subSequence(caseEndPositionIndex, nextStartPositionIndex)
               .toString()
               .trim();
       if (completes && !FALL_THROUGH_PATTERN.matcher(comments).find()) {

This would also fix #613.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants