You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
638 lines
14 KiB
638 lines
14 KiB
10 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
10 years ago
|
|
||
|
package codetoanalyze.java.infer;
|
||
|
|
||
8 years ago
|
import android.annotation.SuppressLint;
|
||
10 years ago
|
import android.content.ContentResolver;
|
||
|
import android.content.Context;
|
||
|
import android.database.Cursor;
|
||
|
import android.database.sqlite.SQLiteDatabase;
|
||
10 years ago
|
import android.text.TextUtils;
|
||
8 years ago
|
import com.facebook.infer.annotation.Assertions;
|
||
9 years ago
|
import com.google.common.base.Optional;
|
||
10 years ago
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.collect.ImmutableList;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
9 years ago
|
import java.nio.channels.FileChannel;
|
||
|
import java.nio.channels.FileLock;
|
||
10 years ago
|
import java.util.HashMap;
|
||
6 years ago
|
import java.util.concurrent.locks.Lock;
|
||
|
import javax.annotation.Nonnull;
|
||
|
import javax.annotation.Nullable;
|
||
10 years ago
|
|
||
|
public class NullPointerExceptions {
|
||
|
|
||
|
class A {
|
||
|
int x;
|
||
|
|
||
6 years ago
|
public void method() {}
|
||
10 years ago
|
}
|
||
|
|
||
|
// npe local with field
|
||
|
public int nullPointerException() {
|
||
|
A a = null;
|
||
|
return a.x;
|
||
|
}
|
||
|
|
||
|
public A canReturnNullObject(boolean ok) {
|
||
|
A a = new A();
|
||
6 years ago
|
if (ok) return a;
|
||
|
else return null;
|
||
10 years ago
|
}
|
||
|
|
||
|
public static void expectNotNullObjectParameter(A a) {
|
||
|
a.method();
|
||
|
}
|
||
|
|
||
|
public static void expectNotNullArrayParameter(A[] array) {
|
||
|
array.clone();
|
||
|
}
|
||
|
|
||
|
// npe with branching, interprocedural
|
||
|
public int nullPointerExceptionInterProc() {
|
||
|
A a = canReturnNullObject(false);
|
||
|
return a.x;
|
||
|
}
|
||
|
|
||
|
// npe with exception handling
|
||
|
public int nullPointerExceptionWithExceptionHandling(boolean ok) {
|
||
|
A a = null;
|
||
|
try {
|
||
|
throw new Exception();
|
||
|
} catch (Exception e) {
|
||
|
return a.x;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class B {
|
||
|
A a;
|
||
|
|
||
6 years ago
|
void test() {}
|
||
10 years ago
|
}
|
||
|
|
||
|
public static int nullPointerExceptionWithArray() {
|
||
6 years ago
|
A[] array = new A[] {null};
|
||
10 years ago
|
A t = array[0];
|
||
|
return t.x;
|
||
|
}
|
||
|
|
||
|
// npe with a chain of fields
|
||
|
class C {
|
||
|
B b;
|
||
|
}
|
||
|
|
||
|
public int nullPointerExceptionWithAChainOfFields(C c) {
|
||
|
c.b = new B();
|
||
|
return c.b.a.x;
|
||
|
}
|
||
|
|
||
|
// npe with a null object parameter
|
||
|
public static void nullPointerExceptionWithNullObjectParameter() {
|
||
|
expectNotNullObjectParameter(null);
|
||
|
}
|
||
|
|
||
|
// npe with a null array parameter
|
||
|
public static void nullPointerExceptionWithNullArrayParameter() {
|
||
|
expectNotNullArrayParameter(null);
|
||
|
}
|
||
|
|
||
|
public static void nullPointerExceptionFromFaillingResourceConstructor() throws IOException {
|
||
|
FileInputStream fis = null;
|
||
|
try {
|
||
|
fis = new FileInputStream(new File("whatever.txt"));
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
fis.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void nullPointerExceptionFromFailingFileOutputStreamConstructor()
|
||
|
throws IOException {
|
||
|
FileOutputStream fos = null;
|
||
|
try {
|
||
|
fos = new FileOutputStream(new File("whatever.txt"));
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int x;
|
||
|
|
||
|
public void nullPointerExceptionFromNotKnowingThatThisIsNotNull() {
|
||
6 years ago
|
if (this == null) {}
|
||
10 years ago
|
this.x = 4;
|
||
|
}
|
||
|
|
||
|
public <T> T id_generics(T o) {
|
||
|
o.toString();
|
||
|
return o;
|
||
|
}
|
||
|
|
||
|
public A frame(A x) {
|
||
|
return id_generics(x);
|
||
|
}
|
||
|
|
||
|
public void nullPointerExceptionUnlessFrameFails() {
|
||
|
String s = null;
|
||
|
Object a = frame(new A());
|
||
|
if (a instanceof A) {
|
||
|
s.length();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class D {
|
||
|
int x;
|
||
|
}
|
||
|
|
||
|
public int preconditionCheckStateTest(D d) {
|
||
|
Preconditions.checkState(d != null);
|
||
|
return d.x;
|
||
|
}
|
||
|
|
||
|
public void genericMethodSomewhereCheckingForNull(String s) {
|
||
6 years ago
|
if (s == null) {}
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
public void FP_noNullPointerExceptionAfterSkipFunction() {
|
||
10 years ago
|
String t = new String("Hello!");
|
||
|
String s = t.toString();
|
||
|
genericMethodSomewhereCheckingForNull(s);
|
||
|
s.length();
|
||
|
}
|
||
|
|
||
|
String hashmapNPE(HashMap h, Object o) {
|
||
|
return (h.get(o).toString());
|
||
|
}
|
||
|
|
||
|
String NPEhashmapProtectedByContainsKey(HashMap h, Object o) {
|
||
|
if (h.containsKey(o)) {
|
||
|
return (h.get(o).toString());
|
||
|
}
|
||
|
return "aa";
|
||
|
}
|
||
|
|
||
6 years ago
|
int NPEvalueOfFromHashmapBad(HashMap<Integer, Integer> h, int position) {
|
||
10 years ago
|
return h.get(position);
|
||
|
}
|
||
|
|
||
6 years ago
|
Integer NPEvalueOfFromHashmapGood(HashMap<Integer, Integer> h, int position) {
|
||
10 years ago
|
return h.get(position);
|
||
|
}
|
||
|
|
||
|
void nullPointerExceptionInArrayLengthLoop(Object[] arr) {
|
||
|
for (int i = 0; i < arr.length; i++) {
|
||
|
Object x = null;
|
||
|
x.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Context mContext;
|
||
|
ContentResolver mContentResolver;
|
||
|
|
||
|
public void cursorFromContentResolverNPE(String customClause) {
|
||
|
String[] projection = {"COUNT(*)"};
|
||
|
String selectionClause = selectionClause = customClause;
|
||
6 years ago
|
Cursor cursor =
|
||
|
mContext.getContentResolver().query(null, projection, selectionClause, null, null);
|
||
10 years ago
|
cursor.close();
|
||
|
}
|
||
|
|
||
|
public int cursorQueryShouldNotReturnNull(SQLiteDatabase sqLiteDatabase) {
|
||
6 years ago
|
Cursor cursor = sqLiteDatabase.query("events", null, null, null, null, null, null);
|
||
|
try {
|
||
|
return cursor.getCount();
|
||
|
} finally {
|
||
|
cursor.close();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
Object[] arr = new Object[1];
|
||
|
|
||
|
Object arrayReadShouldNotCauseSymexMemoryError(int i) {
|
||
|
arr[i].toString();
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
void nullPointerExceptionCallArrayReadMethod() {
|
||
|
arr[0] = new Object();
|
||
|
arrayReadShouldNotCauseSymexMemoryError(0).toString();
|
||
|
}
|
||
|
|
||
|
public void sinkWithNeverNullSource() {
|
||
|
NeverNullSource source = new NeverNullSource();
|
||
|
T t = source.get();
|
||
|
t.f();
|
||
|
}
|
||
|
|
||
|
public void otherSinkWithNeverNullSource() {
|
||
|
SomeLibrary source = new SomeLibrary();
|
||
|
T t = source.get();
|
||
|
t.f();
|
||
|
}
|
||
|
|
||
|
private @Nullable Object mFld;
|
||
|
|
||
|
void nullableFieldNPE() {
|
||
|
mFld.toString();
|
||
|
}
|
||
|
|
||
|
void guardedNullableFieldDeref() {
|
||
|
if (mFld != null) mFld.toString();
|
||
|
}
|
||
|
|
||
|
void allocNullableFieldDeref() {
|
||
|
mFld = new Object();
|
||
|
mFld.toString();
|
||
|
}
|
||
|
|
||
|
void nullableParamNPE(@Nullable Object param) {
|
||
|
param.toString();
|
||
|
}
|
||
|
|
||
|
void guardedNullableParamDeref(@Nullable Object param) {
|
||
|
if (param != null) param.toString();
|
||
|
}
|
||
|
|
||
|
void allocNullableParamDeref(@Nullable Object param) {
|
||
|
param = new Object();
|
||
|
param.toString();
|
||
|
}
|
||
|
|
||
|
native boolean test();
|
||
|
|
||
|
Object getObj() {
|
||
|
if (test()) {
|
||
|
return new Object();
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Boolean getBool() {
|
||
|
if (test()) {
|
||
|
return new Boolean(true);
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void derefGetterAfterCheckShouldNotCauseNPE() {
|
||
|
if (getObj() != null) {
|
||
|
getObj().toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void derefBoxedGetterAfterCheckShouldNotCauseNPE() {
|
||
|
boolean b = getBool() != null && getBool();
|
||
|
}
|
||
|
|
||
|
static void derefNonThisGetterAfterCheckShouldNotCauseNPE() {
|
||
|
NullPointerExceptions c = new NullPointerExceptions();
|
||
|
if (c.getObj() != null) {
|
||
|
c.getObj().toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void badCheckShouldCauseNPE() {
|
||
|
if (getBool() != null) getObj().toString();
|
||
|
}
|
||
|
|
||
|
void nullPointerExceptionArrayLength() {
|
||
|
Object[] arr = null;
|
||
|
int i = arr.length;
|
||
|
}
|
||
|
|
||
10 years ago
|
class $$Class$Name$With$Dollars {
|
||
|
void npeWithDollars() {
|
||
|
String s = null;
|
||
|
int n = s.length();
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
void nullableNonNullStringAfterTextUtilsIsEmptyCheckShouldNotCauseNPE(@Nullable String str) {
|
||
10 years ago
|
if (!TextUtils.isEmpty(str)) {
|
||
10 years ago
|
str.length();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void someNPEAfterResourceLeak() {
|
||
|
T t = CloseableAsResourceExample.sourceOfNullWithResourceLeak();
|
||
|
t.f();
|
||
|
}
|
||
|
|
||
9 years ago
|
private Object mOkObj = new Object();
|
||
|
|
||
|
public void nullableParamReassign1(@Nullable Object o) {
|
||
|
if (o == null) {
|
||
|
o = mOkObj;
|
||
|
}
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
|
public void nullableParamReassign2(@Nullable Object o, Object okObj) {
|
||
|
if (o == null) {
|
||
|
o = okObj;
|
||
|
}
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
|
private @Nullable Object mNullableField;
|
||
|
|
||
|
public void nullableFieldReassign1() {
|
||
|
if (mNullableField == null) {
|
||
|
mNullableField = mOkObj;
|
||
|
}
|
||
|
mNullableField.toString();
|
||
|
}
|
||
|
|
||
|
public void nullableFieldReassign2(Object okObj) {
|
||
|
if (mNullableField == null) {
|
||
|
mNullableField = okObj;
|
||
|
}
|
||
|
mNullableField.toString();
|
||
|
}
|
||
|
|
||
|
public void nullableFieldReassign3(Object param) {
|
||
|
mNullableField = param;
|
||
|
mNullableField.toString();
|
||
|
}
|
||
|
|
||
|
public Object nullableGetter() {
|
||
|
return mNullableField;
|
||
|
}
|
||
|
|
||
|
public void derefNullableGetter() {
|
||
|
Object o = nullableGetter();
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
public @Nullable Object nullableRet(boolean b) {
|
||
|
if (b) {
|
||
|
return null;
|
||
|
}
|
||
|
return new Object();
|
||
|
}
|
||
|
|
||
|
public void derefNullableRet(boolean b) {
|
||
|
Object ret = nullableRet(b);
|
||
|
ret.toString();
|
||
|
}
|
||
|
|
||
|
public void derefNullableRetOK(boolean b) {
|
||
|
Object ret = nullableRet(b);
|
||
|
if (ret != null) {
|
||
|
ret.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public native @Nullable Object undefNullableRet();
|
||
|
|
||
9 years ago
|
public void derefUndefNullableRet() {
|
||
9 years ago
|
Object ret = undefNullableRet();
|
||
|
ret.toString();
|
||
|
}
|
||
|
|
||
|
public void derefUndefNullableRetOK() {
|
||
|
Object ret = undefNullableRet();
|
||
|
if (ret != null) {
|
||
|
ret.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
void assumeUndefNullableIdempotentOk() {
|
||
|
if (undefNullableRet() != null) {
|
||
|
undefNullableRet().toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public Object undefNullableWrapper() {
|
||
|
return undefNullableRet();
|
||
|
}
|
||
|
|
||
|
public void derefUndefNullableRetWrapper() {
|
||
|
undefNullableWrapper().toString();
|
||
|
}
|
||
|
|
||
|
private int returnsThreeOnlyIfRetNotNull(Object obj) {
|
||
|
if (obj == null) {
|
||
|
return 2;
|
||
|
}
|
||
|
return 3;
|
||
|
}
|
||
|
|
||
|
public void testNullablePrecision() {
|
||
|
Object ret = undefNullableRet();
|
||
|
if (returnsThreeOnlyIfRetNotNull(ret) == 3) {
|
||
|
ret.toString(); // shouldn't warn here
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public @Nullable String testSystemGetPropertyArgument() {
|
||
|
String s = System.getProperty(null);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
public void testSystemGetPropertyReturn() {
|
||
|
String s = System.getProperty("");
|
||
|
int n = s.length();
|
||
|
}
|
||
9 years ago
|
|
||
|
Object retUndefined() {
|
||
|
return "".toString(); // toString is a skip function
|
||
|
}
|
||
|
|
||
|
Object derefUndefinedCallee() {
|
||
|
// if retUndefined() is handled incorrectly, we get a symexec_memory_error here
|
||
|
retUndefined().toString();
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
void derefNull() {
|
||
|
// should be NPE, but will not be reported if we handled retUndefined() incorrectly
|
||
|
derefUndefinedCallee().toString();
|
||
|
}
|
||
|
|
||
8 years ago
|
@SuppressLint("NULL_DEREFERENCE")
|
||
9 years ago
|
void shouldNotReportNPE() {
|
||
|
Object o = null;
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
void shouldNotReportOnSkippedSource() {
|
||
|
Object o = SkippedSourceFile.createdBySkippedFile();
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
int nullListFiles(String pathname) {
|
||
|
File dir = new File(pathname);
|
||
|
File[] files = dir.listFiles();
|
||
|
return files.length; // expect possible NullPointerException as files == null is possible
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
native Object unknownFunc();
|
||
|
|
||
7 years ago
|
void nullDerefernceReturnOfSkippedFunctionBad() {
|
||
|
Object object = unknownFunc();
|
||
|
if (object == null) {
|
||
|
object.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
native @Nonnull Object doesNotReturnNull();
|
||
|
|
||
|
void noNPEWhenCallingSkippedNonnullAnnotatedMethodGood() {
|
||
|
Object object = doesNotReturnNull();
|
||
|
if (object == null) {
|
||
|
object.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
Object callUnknownFunc() {
|
||
|
return unknownFunc();
|
||
|
}
|
||
|
|
||
|
void dontReportOnNullableDirectReassignmentToUnknown(@Nullable Object o) {
|
||
|
o = unknownFunc();
|
||
|
o.toString();
|
||
|
}
|
||
|
|
||
|
void dontReportOnNullableIndirectReassignmentToUnknown(@Nullable Object o) {
|
||
|
o = callUnknownFunc();
|
||
|
o.toString();
|
||
|
}
|
||
9 years ago
|
|
||
6 years ago
|
@Nullable
|
||
|
Object wrapUnknownFuncWithNullable() {
|
||
7 years ago
|
return unknownFunc();
|
||
|
}
|
||
|
|
||
|
void deferenceNullableMethodCallingSkippedMethodBad() {
|
||
|
wrapUnknownFuncWithNullable().toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
String nullTryLock(FileChannel chan) throws IOException {
|
||
|
FileLock lock = chan.tryLock();
|
||
|
return lock.toString(); // expect possible NullPointerException as lock == null is possible
|
||
|
}
|
||
|
|
||
9 years ago
|
String tryLockThrows(FileChannel chan) {
|
||
|
try {
|
||
|
FileLock lock = chan.tryLock();
|
||
|
return (lock != null ? lock.toString() : "");
|
||
9 years ago
|
} catch (IOException e) {
|
||
9 years ago
|
Object o = null;
|
||
|
return o.toString(); // expect NullPointerException as tryLock can throw
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
class L {
|
||
|
L next;
|
||
|
}
|
||
|
|
||
|
Object returnsNullAfterLoopOnList(L l) {
|
||
|
while (l != null) {
|
||
|
l = l.next;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
void dereferenceAfterLoopOnList(L l) {
|
||
|
Object obj = returnsNullAfterLoopOnList(l);
|
||
|
obj.toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
void dereferenceAfterUnlock1(Lock l) {
|
||
|
l.unlock();
|
||
|
String s = l.toString();
|
||
|
s = null;
|
||
|
s.toString(); // Expect NPE here
|
||
|
}
|
||
|
|
||
|
void dereferenceAfterUnlock2(Lock l) {
|
||
6 years ago
|
synchronized (l) {
|
||
9 years ago
|
String b = null;
|
||
|
}
|
||
|
String s = l.toString();
|
||
|
s = null;
|
||
|
s.toString(); // Expect NPE here
|
||
|
}
|
||
|
|
||
9 years ago
|
void optionalNPE(Optional<Object> o) {
|
||
|
o.orNull().toString();
|
||
|
}
|
||
|
|
||
9 years ago
|
void stringConstantEqualsTrueNotNPE() {
|
||
|
final String c1 = "Test string!";
|
||
|
final String c2 = "Test string!";
|
||
|
String s = null;
|
||
6 years ago
|
if (c1.equals(c1)) {
|
||
9 years ago
|
s = "safe";
|
||
|
}
|
||
|
s.toString(); // No NPE
|
||
|
s = null;
|
||
6 years ago
|
if (c1.equals(c2)) {
|
||
9 years ago
|
s = "safe";
|
||
|
}
|
||
|
s.toString(); // No NPE
|
||
|
}
|
||
|
|
||
|
void stringConstantEqualsFalseNotNPE_FP() {
|
||
|
// This won't actually cause an NPE, but our current model for String.equals
|
||
|
// returns boolean_undefined for all cases other than String constant
|
||
|
// equality. Consider handling constant inequality in the future.
|
||
|
final String c1 = "Test string 1";
|
||
|
final String c2 = "Test string 2";
|
||
|
String s = null;
|
||
6 years ago
|
if (!c1.equals(c2)) {
|
||
9 years ago
|
s = "safe";
|
||
|
}
|
||
|
s.toString(); // No NPE
|
||
|
}
|
||
|
|
||
|
String getString2() {
|
||
|
return "string 2";
|
||
|
}
|
||
|
|
||
|
void stringVarEqualsFalseNPE() {
|
||
|
final String c1 = "Test string 1";
|
||
|
String c2 = "Test " + getString2();
|
||
|
String s = null;
|
||
6 years ago
|
if (!c1.equals(c2)) {
|
||
9 years ago
|
s.toString(); // NPE
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
String assertParameterNotNullableOk(@Nullable Object object) {
|
||
|
return Assertions.assertNotNull(object).toString();
|
||
|
}
|
||
|
|
||
7 years ago
|
interface I {
|
||
|
@Nullable Object mObject = null;
|
||
|
}
|
||
|
|
||
|
class E implements I {
|
||
|
|
||
|
void dereferenceNullableInterfaceFieldBad() {
|
||
|
mObject.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
Object getObject() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
void addNullToImmutableListBuilderBad() {
|
||
|
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
|
||
|
listBuilder.add(getObject());
|
||
|
}
|
||
10 years ago
|
}
|