Add extra dereference to the variable of the method call to find null dereferences when a method called with null.
Reviewed By: akotulski Differential Revision: D2954284 fb-gh-sync-id: d9079db shipit-source-id: d9079dbmaster
parent
ba4dbef2d9
commit
06f40efab8
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
struct X {
|
||||
int f;
|
||||
int call() { return 1; }
|
||||
};
|
||||
|
||||
int npe_call() {
|
||||
X* x = nullptr;
|
||||
return x->call();
|
||||
}
|
||||
|
||||
X* getX() { return nullptr; }
|
||||
|
||||
void npe_call_after_call() { getX()->call(); }
|
||||
|
||||
struct XForward;
|
||||
|
||||
struct Y {
|
||||
XForward* x;
|
||||
};
|
||||
|
||||
struct XForward {
|
||||
int call() { return 0; }
|
||||
int f;
|
||||
};
|
||||
|
||||
void call_with_forward_declaration(XForward* x) { x->call(); }
|
||||
|
||||
void npe_call_with_forward_declaration() {
|
||||
call_with_forward_declaration(nullptr);
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
digraph iCFG {
|
||||
22 [label="22: Call _fun_call_with_forward_declaration \n _fun_call_with_forward_declaration(null:class XForward *) [line 38]\n APPLY_ABSTRACTION; [line 38]\n " shape="box"]
|
||||
|
||||
|
||||
22 -> 21 ;
|
||||
21 [label="21: Exit npe_call_with_forward_declaration \n " color=yellow style=filled]
|
||||
|
||||
|
||||
20 [label="20: Start npe_call_with_forward_declaration\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 37]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
20 -> 22 ;
|
||||
19 [label="19: Call _fun_XForward_call \n n$0=*&x:class XForward * [line 35]\n n$1=*n$0:class XForward [line 35]\n n$2=_fun_XForward_call(n$0:class XForward *) [line 35]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 35]\n NULLIFY(&x,false); [line 35]\n APPLY_ABSTRACTION; [line 35]\n " shape="box"]
|
||||
|
||||
|
||||
19 -> 18 ;
|
||||
18 [label="18: Exit call_with_forward_declaration \n " color=yellow style=filled]
|
||||
|
||||
|
||||
17 [label="17: Start call_with_forward_declaration\nFormals: x:class XForward *\nLocals: \n DECLARE_LOCALS(&return); [line 35]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
17 -> 19 ;
|
||||
16 [label="16: Return Stmt \n *&return:int =0 [line 31]\n APPLY_ABSTRACTION; [line 31]\n " shape="box"]
|
||||
|
||||
|
||||
16 -> 15 ;
|
||||
15 [label="15: Exit XForward_call \n " color=yellow style=filled]
|
||||
|
||||
|
||||
14 [label="14: Start XForward_call\nFormals: this:class XForward *\nLocals: \n DECLARE_LOCALS(&return); [line 31]\n NULLIFY(&this,false); [line 31]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
14 -> 16 ;
|
||||
13 [label="13: Call _fun_X_call \n n$0=_fun_getX() [line 22]\n n$1=*n$0:class X [line 22]\n n$2=_fun_X_call(n$0:class X *) [line 22]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 22]\n APPLY_ABSTRACTION; [line 22]\n " shape="box"]
|
||||
|
||||
|
||||
13 -> 12 ;
|
||||
12 [label="12: Exit npe_call_after_call \n " color=yellow style=filled]
|
||||
|
||||
|
||||
11 [label="11: Start npe_call_after_call\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 22]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
11 -> 13 ;
|
||||
10 [label="10: Return Stmt \n *&return:class X *=null [line 20]\n APPLY_ABSTRACTION; [line 20]\n " shape="box"]
|
||||
|
||||
|
||||
10 -> 9 ;
|
||||
9 [label="9: Exit getX \n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 [label="8: Start getX\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 20]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 -> 10 ;
|
||||
7 [label="7: DeclStmt \n *&x:class X *=null [line 16]\n " shape="box"]
|
||||
|
||||
|
||||
7 -> 6 ;
|
||||
6 [label="6: Return Stmt \n n$0=*&x:class X * [line 17]\n n$1=*n$0:class X [line 17]\n n$2=_fun_X_call(n$0:class X *) [line 17]\n *&return:int =n$2 [line 17]\n REMOVE_TEMPS(n$0,n$1,n$2); [line 17]\n NULLIFY(&x,false); [line 17]\n APPLY_ABSTRACTION; [line 17]\n " shape="box"]
|
||||
|
||||
|
||||
6 -> 5 ;
|
||||
5 [label="5: Exit npe_call \n " color=yellow style=filled]
|
||||
|
||||
|
||||
4 [label="4: Start npe_call\nFormals: \nLocals: x:class X * \n DECLARE_LOCALS(&return,&x); [line 15]\n NULLIFY(&x,false); [line 15]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
4 -> 7 ;
|
||||
3 [label="3: Return Stmt \n *&return:int =1 [line 12]\n APPLY_ABSTRACTION; [line 12]\n " shape="box"]
|
||||
|
||||
|
||||
3 -> 2 ;
|
||||
2 [label="2: Exit X_call \n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 [label="1: Start X_call\nFormals: this:class X *\nLocals: \n DECLARE_LOCALS(&return); [line 12]\n NULLIFY(&this,false); [line 12]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 -> 3 ;
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package endtoend.cpp;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsErrorInMethod.contains;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.DebuggableTemporaryFolder;
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
import utils.InferRunner;
|
||||
|
||||
public class NPEWhenMethodCallTest {
|
||||
|
||||
public static final String FILE =
|
||||
"infer/tests/codetoanalyze/cpp/errors/npe/method_call.cpp";
|
||||
|
||||
private static ImmutableList<String> inferCmd;
|
||||
|
||||
public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE";
|
||||
|
||||
@ClassRule
|
||||
public static DebuggableTemporaryFolder folder =
|
||||
new DebuggableTemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferCmd = InferRunner.createCPPInferCommand(folder, FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInferRunsOnMethodCallThenNoNPEFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
|
||||
String[] procedures = {"npe_call", "npe_call_after_call", "npe_call_with_forward_declaration"};
|
||||
assertThat(
|
||||
"Results should contain " + NULL_DEREFERENCE,
|
||||
inferResults,
|
||||
containsExactly(
|
||||
NULL_DEREFERENCE,
|
||||
FILE,
|
||||
procedures
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package frontend.cpp;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.ClangFrontendUtils;
|
||||
import utils.DebuggableTemporaryFolder;
|
||||
import utils.InferException;
|
||||
|
||||
public class NPETest {
|
||||
|
||||
|
||||
String methodBasePath = "infer/tests/codetoanalyze/cpp/errors/npe/";
|
||||
|
||||
@Rule
|
||||
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
|
||||
|
||||
void frontendTest(String fileRelative) throws InterruptedException, IOException, InferException {
|
||||
ClangFrontendUtils.createAndCompareCppDotFiles(folder, methodBasePath + fileRelative);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineMethodDotFilesMatch()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
frontendTest("method_call.cpp");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue