From ea0706012050633f55f91d42ffa45252e3a419de Mon Sep 17 00:00:00 2001 From: cushon Date: Thu, 19 Apr 2018 19:21:58 -0700 Subject: [PATCH] Fix a NPE in ByteBufferBackingArray Fixes #1004 MOE_MIGRATED_REVID=193610886 --- .../bugpatterns/ByteBufferBackingArray.java | 3 +++ .../bugpatterns/ByteBufferBackingArrayTest.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ByteBufferBackingArray.java b/core/src/main/java/com/google/errorprone/bugpatterns/ByteBufferBackingArray.java index 5047dfe1f33..9dc1d4dbea6 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ByteBufferBackingArray.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ByteBufferBackingArray.java @@ -87,6 +87,9 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState } while (receiver instanceof MethodInvocationTree); Symbol bufferSymbol = ASTHelpers.getSymbol(receiver); + if (bufferSymbol == null) { + return Description.NO_MATCH; + } // Checks for validating use on method scope. if (bufferSymbol.owner instanceof MethodSymbol) { diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ByteBufferBackingArrayTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ByteBufferBackingArrayTest.java index 66ac634d306..bda02d45e89 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ByteBufferBackingArrayTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ByteBufferBackingArrayTest.java @@ -43,4 +43,18 @@ public void testNegativeCases() throws Exception { compilationHelper.addSourceFile("ByteBufferBackingArrayNegativeCases.java").doTest(); } + + @Test + public void i1004() { + compilationHelper + .addSourceLines( + "Test.java", + "import java.nio.ByteBuffer;", + "public class Test {", + " public void ByteBufferBackingArrayTest() {", + " byte[] byteArray = ((ByteBuffer) new Object()).array();", + " }", + "}") + .doTest(); + } }