Reviewed By: akotulski Differential Revision: D2943884 fb-gh-sync-id: 33b1c8b shipit-source-id: 33b1c8bmaster
parent
c585383fba
commit
2fb2b3d9e1
@ -1 +1 @@
|
||||
Subproject commit d07910830a3ccf56c10a4eaf9c8dcad23ecb4d9b
|
||||
Subproject commit 60b10ef73606e814cf87c08e5c0d263dc1f9fb8c
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
class Polygon {
|
||||
protected:
|
||||
int width, height;
|
||||
|
||||
public:
|
||||
virtual ~Polygon();
|
||||
void set_values(int a, int b) {
|
||||
width = a;
|
||||
height = b;
|
||||
}
|
||||
virtual int area() { return 0; }
|
||||
};
|
||||
|
||||
class Rectangle : public Polygon {
|
||||
public:
|
||||
~Rectangle();
|
||||
int area() { return width * height; }
|
||||
};
|
||||
|
||||
class Triangle : public Polygon {
|
||||
public:
|
||||
~Triangle(){};
|
||||
int area() {
|
||||
int x = width * height;
|
||||
return x - 10;
|
||||
}
|
||||
};
|
||||
|
||||
int rect_area() {
|
||||
Rectangle rect;
|
||||
Polygon* ppoly1 = ▭
|
||||
ppoly1->set_values(4, 5);
|
||||
return 1 / (ppoly1->area() - 20);
|
||||
}
|
||||
|
||||
int tri_area() {
|
||||
Triangle trgl;
|
||||
Polygon poly;
|
||||
Polygon* ppoly2 = &trgl;
|
||||
ppoly2->set_values(4, 5);
|
||||
return 1 / (ppoly2->area() - 10);
|
||||
}
|
||||
|
||||
int poly_area() {
|
||||
Polygon poly;
|
||||
Polygon* ppoly3 = &poly;
|
||||
return 1 / ppoly3->area();
|
||||
}
|
||||
|
||||
int tri_not_virtual_area() {
|
||||
Triangle trgl;
|
||||
Polygon poly;
|
||||
Polygon* ppoly2 = &trgl;
|
||||
ppoly2->set_values(4, 5);
|
||||
return 1 / ppoly2->Polygon::area();
|
||||
}
|
||||
|
||||
// dynamic dispatch in this case still doesn't work,
|
||||
// one need special dealing with names for destructors.
|
||||
// But the call gets the virtual flag.
|
||||
void call_virtual_destructor() {
|
||||
Polygon* trgl = new Triangle;
|
||||
delete trgl;
|
||||
}
|
@ -0,0 +1,201 @@
|
||||
digraph iCFG {
|
||||
53 [label="53: DeclStmt \n n$1=_fun___new(sizeof(class Triangle ):unsigned long ) [line 71]\n *&trgl:class Triangle *=n$1 [line 71]\n REMOVE_TEMPS(n$1); [line 71]\n " shape="box"]
|
||||
|
||||
|
||||
53 -> 52 ;
|
||||
52 [label="52: Call delete \n n$0=*&trgl:class Polygon * [line 72]\n _fun_Polygon_~Polygon(n$0:class Polygon *) virtual [line 72]\n _fun___delete(n$0:class Polygon *) [line 72]\n REMOVE_TEMPS(n$0); [line 72]\n NULLIFY(&trgl,false); [line 72]\n APPLY_ABSTRACTION; [line 72]\n " shape="box"]
|
||||
|
||||
|
||||
52 -> 51 ;
|
||||
51 [label="51: Exit call_virtual_destructor \n " color=yellow style=filled]
|
||||
|
||||
|
||||
50 [label="50: Start call_virtual_destructor\nFormals: \nLocals: trgl:class Polygon * \n DECLARE_LOCALS(&return,&trgl); [line 70]\n NULLIFY(&trgl,false); [line 70]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
50 -> 53 ;
|
||||
49 [label="49: DeclStmt \n _fun_Triangle_Triangle(&trgl:class Triangle *) [line 60]\n " shape="box"]
|
||||
|
||||
|
||||
49 -> 48 ;
|
||||
48 [label="48: DeclStmt \n _fun_Polygon_Polygon(&poly:class Polygon *) [line 61]\n " shape="box"]
|
||||
|
||||
|
||||
48 -> 47 ;
|
||||
47 [label="47: DeclStmt \n *&ppoly2:class Triangle *=&trgl [line 62]\n " shape="box"]
|
||||
|
||||
|
||||
47 -> 46 ;
|
||||
46 [label="46: Call _fun_Polygon_set_values \n n$2=*&ppoly2:class Polygon * [line 63]\n _fun_Polygon_set_values(n$2:class Polygon *,4:int ,5:int ) [line 63]\n REMOVE_TEMPS(n$2); [line 63]\n " shape="box"]
|
||||
|
||||
|
||||
46 -> 45 ;
|
||||
45 [label="45: Return Stmt \n n$0=*&ppoly2:class Polygon * [line 64]\n n$1=_fun_Polygon_area(n$0:class Polygon *) [line 64]\n *&return:int =(1 / n$1) [line 64]\n REMOVE_TEMPS(n$0,n$1); [line 64]\n NULLIFY(&ppoly2,false); [line 64]\n NULLIFY(&poly,false); [line 64]\n NULLIFY(&trgl,false); [line 64]\n APPLY_ABSTRACTION; [line 64]\n " shape="box"]
|
||||
|
||||
|
||||
45 -> 44 ;
|
||||
44 [label="44: Exit tri_not_virtual_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
43 [label="43: Start tri_not_virtual_area\nFormals: \nLocals: ppoly2:class Polygon * poly:class Polygon trgl:class Triangle \n DECLARE_LOCALS(&return,&ppoly2,&poly,&trgl); [line 59]\n NULLIFY(&ppoly2,false); [line 59]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
43 -> 49 ;
|
||||
42 [label="42: DeclStmt \n _fun_Polygon_Polygon(&poly:class Polygon *) [line 54]\n " shape="box"]
|
||||
|
||||
|
||||
42 -> 41 ;
|
||||
41 [label="41: DeclStmt \n *&ppoly3:class Polygon *=&poly [line 55]\n " shape="box"]
|
||||
|
||||
|
||||
41 -> 40 ;
|
||||
40 [label="40: Return Stmt \n n$0=*&ppoly3:class Polygon * [line 56]\n n$1=_fun_Polygon_area(n$0:class Polygon *) virtual [line 56]\n *&return:int =(1 / n$1) [line 56]\n REMOVE_TEMPS(n$0,n$1); [line 56]\n NULLIFY(&ppoly3,false); [line 56]\n NULLIFY(&poly,false); [line 56]\n APPLY_ABSTRACTION; [line 56]\n " shape="box"]
|
||||
|
||||
|
||||
40 -> 39 ;
|
||||
39 [label="39: Exit poly_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
38 [label="38: Start poly_area\nFormals: \nLocals: ppoly3:class Polygon * poly:class Polygon \n DECLARE_LOCALS(&return,&ppoly3,&poly); [line 53]\n NULLIFY(&ppoly3,false); [line 53]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
38 -> 42 ;
|
||||
37 [label="37: DeclStmt \n _fun_Triangle_Triangle(&trgl:class Triangle *) [line 46]\n " shape="box"]
|
||||
|
||||
|
||||
37 -> 36 ;
|
||||
36 [label="36: DeclStmt \n _fun_Polygon_Polygon(&poly:class Polygon *) [line 47]\n " shape="box"]
|
||||
|
||||
|
||||
36 -> 35 ;
|
||||
35 [label="35: DeclStmt \n *&ppoly2:class Triangle *=&trgl [line 48]\n " shape="box"]
|
||||
|
||||
|
||||
35 -> 34 ;
|
||||
34 [label="34: Call _fun_Polygon_set_values \n n$2=*&ppoly2:class Polygon * [line 49]\n _fun_Polygon_set_values(n$2:class Polygon *,4:int ,5:int ) [line 49]\n REMOVE_TEMPS(n$2); [line 49]\n " shape="box"]
|
||||
|
||||
|
||||
34 -> 33 ;
|
||||
33 [label="33: Return Stmt \n n$0=*&ppoly2:class Polygon * [line 50]\n n$1=_fun_Polygon_area(n$0:class Polygon *) virtual [line 50]\n *&return:int =(1 / (n$1 - 10)) [line 50]\n REMOVE_TEMPS(n$0,n$1); [line 50]\n NULLIFY(&ppoly2,false); [line 50]\n NULLIFY(&poly,false); [line 50]\n NULLIFY(&trgl,false); [line 50]\n APPLY_ABSTRACTION; [line 50]\n " shape="box"]
|
||||
|
||||
|
||||
33 -> 32 ;
|
||||
32 [label="32: Exit tri_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
31 [label="31: Start tri_area\nFormals: \nLocals: ppoly2:class Polygon * poly:class Polygon trgl:class Triangle \n DECLARE_LOCALS(&return,&ppoly2,&poly,&trgl); [line 45]\n NULLIFY(&ppoly2,false); [line 45]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
31 -> 37 ;
|
||||
30 [label="30: DeclStmt \n _fun_Rectangle_Rectangle(&rect:class Rectangle *) [line 39]\n " shape="box"]
|
||||
|
||||
|
||||
30 -> 29 ;
|
||||
29 [label="29: DeclStmt \n *&ppoly1:class Rectangle *=&rect [line 40]\n " shape="box"]
|
||||
|
||||
|
||||
29 -> 28 ;
|
||||
28 [label="28: Call _fun_Polygon_set_values \n n$2=*&ppoly1:class Polygon * [line 41]\n _fun_Polygon_set_values(n$2:class Polygon *,4:int ,5:int ) [line 41]\n REMOVE_TEMPS(n$2); [line 41]\n " shape="box"]
|
||||
|
||||
|
||||
28 -> 27 ;
|
||||
27 [label="27: Return Stmt \n n$0=*&ppoly1:class Polygon * [line 42]\n n$1=_fun_Polygon_area(n$0:class Polygon *) virtual [line 42]\n *&return:int =(1 / (n$1 - 20)) [line 42]\n REMOVE_TEMPS(n$0,n$1); [line 42]\n NULLIFY(&ppoly1,false); [line 42]\n NULLIFY(&rect,false); [line 42]\n APPLY_ABSTRACTION; [line 42]\n " shape="box"]
|
||||
|
||||
|
||||
27 -> 26 ;
|
||||
26 [label="26: Exit rect_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
25 [label="25: Start rect_area\nFormals: \nLocals: ppoly1:class Polygon * rect:class Rectangle \n DECLARE_LOCALS(&return,&ppoly1,&rect); [line 38]\n NULLIFY(&ppoly1,false); [line 38]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
25 -> 30 ;
|
||||
24 [label="24: Constructor Init \n n$0=*&this:class Triangle * [line 29]\n _fun_Polygon_Polygon(n$0:class Triangle *) [line 29]\n REMOVE_TEMPS(n$0); [line 29]\n NULLIFY(&this,false); [line 29]\n APPLY_ABSTRACTION; [line 29]\n " shape="box"]
|
||||
|
||||
|
||||
24 -> 23 ;
|
||||
23 [label="23: Exit Triangle_Triangle \n " color=yellow style=filled]
|
||||
|
||||
|
||||
22 [label="22: Start Triangle_Triangle\nFormals: this:class Triangle *\nLocals: \n DECLARE_LOCALS(&return); [line 29]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
22 -> 24 ;
|
||||
21 [label="21: DeclStmt \n n$1=*&this:class Triangle * [line 33]\n n$2=*n$1.width:int [line 33]\n n$3=*&this:class Triangle * [line 33]\n n$4=*n$3.height:int [line 33]\n *&x:int =(n$2 * n$4) [line 33]\n REMOVE_TEMPS(n$1,n$2,n$3,n$4); [line 33]\n NULLIFY(&this,false); [line 33]\n " shape="box"]
|
||||
|
||||
|
||||
21 -> 20 ;
|
||||
20 [label="20: Return Stmt \n n$0=*&x:int [line 34]\n *&return:int =(n$0 - 10) [line 34]\n REMOVE_TEMPS(n$0); [line 34]\n NULLIFY(&x,false); [line 34]\n APPLY_ABSTRACTION; [line 34]\n " shape="box"]
|
||||
|
||||
|
||||
20 -> 19 ;
|
||||
19 [label="19: Exit Triangle_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
18 [label="18: Start Triangle_area\nFormals: this:class Triangle *\nLocals: x:int \n DECLARE_LOCALS(&return,&x); [line 32]\n NULLIFY(&x,false); [line 32]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
18 -> 21 ;
|
||||
17 [label="17: Exit Triangle_~Triangle \n " color=yellow style=filled]
|
||||
|
||||
|
||||
16 [label="16: Start Triangle_~Triangle\nFormals: this:class Triangle *\nLocals: \n DECLARE_LOCALS(&return); [line 31]\n NULLIFY(&this,false); [line 31]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
16 -> 17 ;
|
||||
15 [label="15: Constructor Init \n n$0=*&this:class Rectangle * [line 23]\n _fun_Polygon_Polygon(n$0:class Rectangle *) [line 23]\n REMOVE_TEMPS(n$0); [line 23]\n NULLIFY(&this,false); [line 23]\n APPLY_ABSTRACTION; [line 23]\n " shape="box"]
|
||||
|
||||
|
||||
15 -> 14 ;
|
||||
14 [label="14: Exit Rectangle_Rectangle \n " color=yellow style=filled]
|
||||
|
||||
|
||||
13 [label="13: Start Rectangle_Rectangle\nFormals: this:class Rectangle *\nLocals: \n DECLARE_LOCALS(&return); [line 23]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
13 -> 15 ;
|
||||
12 [label="12: Return Stmt \n n$0=*&this:class Rectangle * [line 26]\n n$1=*n$0.width:int [line 26]\n n$2=*&this:class Rectangle * [line 26]\n n$3=*n$2.height:int [line 26]\n *&return:int =(n$1 * n$3) [line 26]\n REMOVE_TEMPS(n$0,n$1,n$2,n$3); [line 26]\n NULLIFY(&this,false); [line 26]\n APPLY_ABSTRACTION; [line 26]\n " shape="box"]
|
||||
|
||||
|
||||
12 -> 11 ;
|
||||
11 [label="11: Exit Rectangle_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
10 [label="10: Start Rectangle_area\nFormals: this:class Rectangle *\nLocals: \n DECLARE_LOCALS(&return); [line 26]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
10 -> 12 ;
|
||||
9 [label="9: Exit Polygon_Polygon \n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 [label="8: Start Polygon_Polygon\nFormals: this:class Polygon *\nLocals: \n DECLARE_LOCALS(&return); [line 10]\n NULLIFY(&this,false); [line 10]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
8 -> 9 ;
|
||||
7 [label="7: Return Stmt \n *&return:int =0 [line 20]\n APPLY_ABSTRACTION; [line 20]\n " shape="box"]
|
||||
|
||||
|
||||
7 -> 6 ;
|
||||
6 [label="6: Exit Polygon_area \n " color=yellow style=filled]
|
||||
|
||||
|
||||
5 [label="5: Start Polygon_area\nFormals: this:class Polygon *\nLocals: \n DECLARE_LOCALS(&return); [line 20]\n NULLIFY(&this,false); [line 20]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
5 -> 7 ;
|
||||
4 [label="4: BinaryOperatorStmt: Assign \n n$2=*&this:class Polygon * [line 17]\n n$3=*&a:int [line 17]\n *n$2.width:int =n$3 [line 17]\n REMOVE_TEMPS(n$2,n$3); [line 17]\n NULLIFY(&a,false); [line 17]\n " shape="box"]
|
||||
|
||||
|
||||
4 -> 3 ;
|
||||
3 [label="3: BinaryOperatorStmt: Assign \n n$0=*&this:class Polygon * [line 18]\n n$1=*&b:int [line 18]\n *n$0.height:int =n$1 [line 18]\n REMOVE_TEMPS(n$0,n$1); [line 18]\n NULLIFY(&b,false); [line 18]\n NULLIFY(&this,false); [line 18]\n APPLY_ABSTRACTION; [line 18]\n " shape="box"]
|
||||
|
||||
|
||||
3 -> 2 ;
|
||||
2 [label="2: Exit Polygon_set_values \n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 [label="1: Start Polygon_set_values\nFormals: this:class Polygon * a:int b:int \nLocals: \n DECLARE_LOCALS(&return); [line 16]\n " color=yellow style=filled]
|
||||
|
||||
|
||||
1 -> 4 ;
|
||||
}
|
@ -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.ResultContainsExactly.containsExactly;
|
||||
|
||||
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 VirtualMethodsTest {
|
||||
|
||||
public static final String FILE =
|
||||
"infer/tests/codetoanalyze/cpp/frontend/methods/virtual_methods.cpp";
|
||||
|
||||
private static ImmutableList<String> inferCmd;
|
||||
|
||||
public static final String DIVIDE_BY_ZERO = "DIVIDE_BY_ZERO";
|
||||
|
||||
@ClassRule
|
||||
public static DebuggableTemporaryFolder folder =
|
||||
new DebuggableTemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferCmd = InferRunner.createCPPInferCommand(folder, FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInferRunsOnMethodsDivideByZeroIsFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
|
||||
String[] proceduresWithNullDeref = {
|
||||
"rect_area", "tri_area", "poly_area", "tri_not_virtual_area"
|
||||
};
|
||||
assertThat(
|
||||
"Results should contain the no null dereference",
|
||||
inferResults,
|
||||
containsExactly(
|
||||
DIVIDE_BY_ZERO,
|
||||
FILE,
|
||||
proceduresWithNullDeref
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue