From b729c93dd5506308339a5a0f898c998417c71afd Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Sat, 12 Dec 2015 08:47:48 -0800 Subject: [PATCH] Add model of java.lang.Class.getResource Summary: public It is possible to return null according to http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String). Also, getResource throws NPE if passed null: $ cat -n TestClassGetResourceArgument.java 1 import java.net.URL; 2 3 public class TestClassGetResourceArgument { 4 5 static URL testClassGetResourceArgument(Class cls) { 6 return cls.getResource(null); 7 } 8 9 public static void main(String[] args) { 10 System.out.println(testClassGetResourceArgument("".getClass()).toString()); 11 } 12 13 } $ javac TestClassGetResourceArgument.java && java TestClassGetResourceArgument Exception in thread "main" java.lang.NullPointerException at sun.misc.MetaIndex.mayContain(MetaIndex.java:243) at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:830) at sun.misc.URLClassPath.getResource(URLClassPath.java:199) at sun.misc.URLClassPath.getResource(URLClassPath.java:251) at java.lang.ClassLoader.getBootstrapResource(ClassLoader.java:1305) at java.lang.ClassLoader.getResource(ClassLoader.java:1144) at java.lang.ClassLoader.getResource(ClassLoader.java:1142) at java.lang.ClassLoader.getSystemResource(ClassLoader.java:1267) at java.lang.Class.getResource(Class.java:2145) at TestClassGetResourceArgument.testClassGetResourceArgument(TestClassGetResourceArgument.java:6) at TestClassGetResourceArgument.main(TestClassGetResourceArgument.java:10) Reviewed By: cristianoc Differential Revision: D2752301 fb-gh-sync-id: 888baf1 --- infer/src/checkers/modelTables.ml | 1 + .../codetoanalyze/java/eradicate/ParameterNotNullable.java | 5 +++++ .../codetoanalyze/java/eradicate/ReturnNotNullable.java | 6 ++++++ .../endtoend/java/eradicate/ParameterNotNullableTest.java | 1 + .../endtoend/java/eradicate/ReturnNotNullableTest.java | 1 + 5 files changed, 14 insertions(+) diff --git a/infer/src/checkers/modelTables.ml b/infer/src/checkers/modelTables.ml index 51f005b85..1f5d33e3c 100644 --- a/infer/src/checkers/modelTables.ml +++ b/infer/src/checkers/modelTables.ml @@ -140,6 +140,7 @@ let annotated_list_nullable = o1, "com.google.common.util.concurrent.SettableFuture.setException(java.lang.Throwable):boolean"; o1, "java.io.File.(java.lang.String)"; n1, "java.io.PrintStream.print(java.lang.String):void"; + (n, [o]), "java.lang.Class.getResource(java.lang.String):java.net.URL"; o1, "java.lang.Class.isAssignableFrom(java.lang.Class):boolean"; n1, "java.lang.Integer.equals(java.lang.Object):boolean"; n2, "java.lang.RuntimeException.(java.lang.String,java.lang.Throwable)"; diff --git a/infer/tests/codetoanalyze/java/eradicate/ParameterNotNullable.java b/infer/tests/codetoanalyze/java/eradicate/ParameterNotNullable.java index 5c5c413c9..1823476a2 100644 --- a/infer/tests/codetoanalyze/java/eradicate/ParameterNotNullable.java +++ b/infer/tests/codetoanalyze/java/eradicate/ParameterNotNullable.java @@ -10,6 +10,7 @@ package codetoanalyze.java.eradicate; import java.lang.System; +import java.net.URL; import javax.annotation.Nullable; import android.annotation.SuppressLint; @@ -71,4 +72,8 @@ public class ParameterNotNullable { return s; } + static @Nullable URL testClassGetResourceArgument(Class cls) { + return cls.getResource(null); + } + } diff --git a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java index e6c902d2c..22fc245a1 100644 --- a/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java +++ b/infer/tests/codetoanalyze/java/eradicate/ReturnNotNullable.java @@ -11,6 +11,7 @@ package codetoanalyze.java.eradicate; import com.google.common.base.Optional; +import java.net.URL; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -115,4 +116,9 @@ public class ReturnNotNullable { return null; } } + + URL getResourceNullable(Class cls, String name) { + return cls.getResource(name); + } + } diff --git a/infer/tests/endtoend/java/eradicate/ParameterNotNullableTest.java b/infer/tests/endtoend/java/eradicate/ParameterNotNullableTest.java index efc39b155..7fe015a31 100644 --- a/infer/tests/endtoend/java/eradicate/ParameterNotNullableTest.java +++ b/infer/tests/endtoend/java/eradicate/ParameterNotNullableTest.java @@ -43,6 +43,7 @@ public class ParameterNotNullableTest { "callNull", "callNullable", "testSystemGetPropertyArgument", + "testClassGetResourceArgument", }; assertThat( "Results should contain " + PARAMETER_NOT_NULLABLE, diff --git a/infer/tests/endtoend/java/eradicate/ReturnNotNullableTest.java b/infer/tests/endtoend/java/eradicate/ReturnNotNullableTest.java index 238c2859f..1209b98f2 100644 --- a/infer/tests/endtoend/java/eradicate/ReturnNotNullableTest.java +++ b/infer/tests/endtoend/java/eradicate/ReturnNotNullableTest.java @@ -58,6 +58,7 @@ public class ReturnNotNullableTest { "returnNullable", "return_null_in_catch", "return_null_in_catch_after_throw", + "getResourceNullable", }; List errorPatterns = createPatterns( RETURN_NOT_NULLABLE,