Skip to content

Instantly share code, notes, and snippets.

@keigoi
Last active November 21, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keigoi/1cd60e92d3c1df0f7f59cdc53f7e5ec5 to your computer and use it in GitHub Desktop.
Save keigoi/1cd60e92d3c1df0f7f59cdc53f7e5ec5 to your computer and use it in GitHub Desktop.
// check this with facebook infer 0.9.4
int main(void) {
int arr[] = {1, 1};
int* p = 0;
for(int i = 0; i<2; i++)
{
if(arr[0] == 2) // always false
{
// hello.c:12: error: NULL_DEREFERENCE
// pointer p last assigned on line 4 could be null and is dereferenced at line 12, column 7
*p = 42;
}
}
return 0;
}
// check this with facebook infer 0.9.4
public class Hello {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
Object n = null;
Object[] arr = {o1, o1, o1, o1, o1};
for(int i=0; i<arr.length; i++) {
if(arr[i].equals(o2)) {
n.equals(n); // never reached but reported as an error
}
}
}
}
/*
import java.util.ArrayList;
public class Hello {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
Object n = null;
ArrayList<Object> arr = new ArrayList<>();
arr.add(o1);
arr.add(o1);
arr.add(o1);
arr.add(o1);
arr.add(o1);
for(Object o : arr) {
if(o.equals(o2)) {
n.equals(n);
}
}
}
}
*/
// check this with facebook infer 0.9.4
public class Hello2 {
public static Object f() {
// int[] arr = new int[]{1, 1}; // (1) abstraction is applied just before the loop
Object n = new Object();
for(int i=0; i < 2; i++) {
int[] arr = new int[]{1, 1}; // (2) no abstraction applied
if(arr[1] == 2) {
n = null; // Infer judges here non-reacheable if (2) is present
}
}
return n;
}
public static void main(String[] args) {
f().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment