[demo] add more examples

Summary: some examples of bugs infer finds

Reviewed By: jeremydubreil

Differential Revision: D15081483

fbshipit-source-id: 4e40c7384
master
Jules Villard 6 years ago committed by Facebook Github Bot
parent da13e52b27
commit 596ffad8dd

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = simple_null.cpp
OBJECTS = $(SOURCES:.cpp=.o)
all: $(OBJECTS)
.cpp.o:
${CC} -c $<
clean:
rm -rf $(OBJECTS)

@ -0,0 +1,10 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
void deref_null() {
int* p = nullptr;
*p = 42;
}

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = simple_null_interproc.c wrappers.c
OBJECTS = $(SOURCES:.c=.o)
all: $(OBJECTS)
.cpp.o:
${CC} -c $<
clean:
rm -rf $(OBJECTS)

@ -0,0 +1,15 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdlib.h>
int* allocate_int();
void set(int* p, int value);
void deref_null() {
int* p = allocate_int();
set(p, 42);
}

@ -0,0 +1,19 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdlib.h>
int* allocate_int();
void set(int* p, int value);
void deref_null_ok() {
int* p = allocate_int();
if (p != NULL) {
set(p, 42);
}
free(p);
}

@ -0,0 +1,11 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdlib.h>
int* allocate_int() { return malloc(sizeof(int)); }
void set(int* p, int value) { *p = value; }

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = Resources.java
OBJECT = Resources.class
all: $(OBJECT)
$(OBJECT): $(SOURCES)
javac $^
clean:
rm -rf $(OBJECT)

@ -0,0 +1,26 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class Resources {
public static void cat() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("infile.txt"));
fos = new FileOutputStream(new File("outfile.txt"));
fos.write(fis.read());
} finally {
if (fis != null) fis.close();
if (fos != null) fos.close();
}
}
}

@ -0,0 +1,20 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ResourcesFixed {
public static void cat() throws IOException {
try (FileInputStream fis = new FileInputStream(new File("infile.txt"));
FileOutputStream fos = new FileOutputStream(new File("outfile.txt")); ) {
fos.write(fis.read());
}
}
}

@ -0,0 +1,12 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@ThreadSafe
public interface Account {
void deposit(int amount);
int withdraw(int amount);
}

@ -0,0 +1,30 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class AccountImplementation implements Account {
int mBalance = 0;
private void addToBalance(int amount) {
mBalance += amount;
}
public synchronized void deposit(int amount) {
if (amount > 0) {
addToBalance(amount);
}
}
public synchronized int withdraw(int amount){
if (amount >= 0 && mBalance - amount >= 0) {
addToBalance(-amount);
return mBalance;
} else {
return 0;
}
}
}

@ -0,0 +1,19 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@ThreadSafe
public class Client {
Account account;
void getPaid(int amount) {
account.deposit(amount);
}
int buyCoffee() {
return account.withdraw(5);
}
}

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = Account.java AccountImplementation.java Client.java
OBJECT = Client.class
all: $(OBJECT)
$(OBJECT): $(SOURCES)
javac $^
clean:
rm -rf $(OBJECT)

@ -0,0 +1,7 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public @interface ThreadSafe {}

@ -0,0 +1,10 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public interface Account {
void deposit(int amount);
int withdraw(int amount);
}

@ -0,0 +1,30 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class AccountImplementation implements Account {
int mBalance = 0;
private void addToBalance(int amount) {
mBalance += amount;
}
public void deposit(int amount) {
if (amount > 0) {
addToBalance(amount);
}
}
public int withdraw(int amount){
if (amount >= 0 && mBalance - amount >= 0) {
addToBalance(-amount);
return mBalance;
} else {
return 0;
}
}
}

@ -0,0 +1,19 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@ThreadSafe
public class Client {
Account account;
void getPaid(int amount) {
account.deposit(amount);
}
int buyCoffee() {
return account.withdraw(5);
}
}

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = Account.java AccountImplementation.java Client.java
OBJECT = Client.class
all: $(OBJECT)
$(OBJECT): $(SOURCES)
javac $^
clean:
rm -rf $(OBJECT)

@ -0,0 +1,7 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public @interface ThreadSafe {}

@ -0,0 +1,15 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public class Client<T> {
String foo() {
Library<T> lib = new Library<T>();
T t = lib.get();
return t == null? "default": t.toString();
}
}

@ -0,0 +1,21 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public class Library<T> {
@Nullable T t;
private native @Nullable T mayReturnNull();
public Library() {
t = mayReturnNull();
}
@Nullable public T get() {
return t;
}
}

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = Client.java Library.java
OBJECT = Client.class
all: $(OBJECT)
$(OBJECT): $(SOURCES)
javac $^
clean:
rm -rf $(OBJECT)

@ -0,0 +1,8 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public @interface Nullable {}

@ -0,0 +1,14 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public class Client<T> {
String foo() {
Library<T> lib = new Library<T>();
return lib.get().toString();
}
}

@ -0,0 +1,21 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public class Library<T> {
T t;
private native @Nullable T mayReturnNull();
public Library() {
t = mayReturnNull();
}
public T get() {
return t;
}
}

@ -0,0 +1,15 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SOURCES = Client.java Library.java
OBJECT = Client.class
all: $(OBJECT)
$(OBJECT): $(SOURCES)
javac $^
clean:
rm -rf $(OBJECT)

@ -0,0 +1,8 @@
/*
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
public @interface Nullable {}

@ -0,0 +1,14 @@
# Copyright (c) 2019-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
SUBDIRS = 00 01 02 03
default: all
clean:
$(foreach subdir,$(SUBDIRS),$(MAKE) -C $(subdir) clean;)
rm -fr $(foreach subdir,$(SUBDIRS),$(subdir)/infer-out/)
all: $(OBJECTS)

@ -0,0 +1,16 @@
#!/bin/bash
# Copyright (c) 2019-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
set -e
infer -- make -C 00 clean all
infer -- make -C 01 clean all
infer explore --no-source-preview --select 1
infer -- make -C 02 clean all
infer explore --no-source-preview --select 0
infer -- make -C 03 clean all
infer explore --no-source-preview --select 1
infer --eradicate-only -- make -C 04 clean all
Loading…
Cancel
Save