From 731c6cdb0a6eb4e72c41a8dd149556fea9fa01c9 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Thu, 10 Dec 2015 09:31:03 -0800 Subject: [PATCH] add interprocedural Java example Reviewed By: cristianoc Differential Revision: D2744378 fb-gh-sync-id: 0ef57a7 --- examples/README | 8 ++-- examples/java_hello/Hello.java | 64 ++++++++++++++++++++++++++++++ examples/java_hello/Pointers.java | 26 ++++++++++++ examples/java_hello/Resources.java | 27 +++++++++++++ 4 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 examples/java_hello/Hello.java create mode 100644 examples/java_hello/Pointers.java create mode 100644 examples/java_hello/Resources.java diff --git a/examples/README b/examples/README index 36714506b..1c45dc712 100644 --- a/examples/README +++ b/examples/README @@ -23,12 +23,14 @@ Contents Make sure that you have the Android SDK 22 installed and up to date, and in particular the "Android SDK Build-tools" and "Android Support Repository". -- ios_hello/: a sample iOS app. Try this example by running - infer -- xcodebuild -target HelloWorldApp -configuration Debug -sdk iphonesimulator - - c_hello/: a sample make-based C project. Try this example by running infer -- make +- ios_hello/: a sample iOS app. Try this example by running + infer -- xcodebuild -target HelloWorldApp -configuration Debug -sdk iphonesimulator + +- java_hello/: a sample Java project. Try this example by running + infer -- javac Pointers.java Resources.java Hello.java Note ---- diff --git a/examples/java_hello/Hello.java b/examples/java_hello/Hello.java new file mode 100644 index 000000000..62803ae64 --- /dev/null +++ b/examples/java_hello/Hello.java @@ -0,0 +1,64 @@ +/* +* 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 hello; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Random; + +class Hello { + + void doesNotCauseNPE() { + Pointers.A a = Pointers.mayReturnNull(10); + a.method(); + } + + void mayCauseNPE() { + Random rng = new Random(); + Pointers.A a = Pointers.mayReturnNull(rng.nextInt()); + // FIXME: should check for null before calling method() + a.method(); + } + + void mayLeakResource() throws IOException { + OutputStream stream = Resources.allocateResource(); + if (stream == null) { + return; + } + + try { + stream.write(12); + } finally { + // FIXME: should close the stream + } + } + + /** + * This method should be rewritten with nested try { ... } finally { + * ... } statements, or the possible exception raised by fis.close() + * should be swallowed. + */ + void twoResources() throws IOException { + FileInputStream fis = null; + FileOutputStream fos = null; + try { + fis = new FileInputStream(new File("whatever.txt")); + fos = new FileOutputStream(new File("everwhat.txt")); + fos.write(fis.read()); + } finally { + if (fis != null) { fis.close(); } + if (fos != null) { fos.close(); } + } + } + +} diff --git a/examples/java_hello/Pointers.java b/examples/java_hello/Pointers.java new file mode 100644 index 000000000..2ea1ff605 --- /dev/null +++ b/examples/java_hello/Pointers.java @@ -0,0 +1,26 @@ +/* +* 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 hello; + +public class Pointers { + + public static class A { + public void method() { + } + } + + public static A mayReturnNull(int i) { + if (i > 0) { + return new A(); + } + return null; + } + +} diff --git a/examples/java_hello/Resources.java b/examples/java_hello/Resources.java new file mode 100644 index 000000000..10316fbd4 --- /dev/null +++ b/examples/java_hello/Resources.java @@ -0,0 +1,27 @@ +/* +* 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 hello; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +public class Resources { + + public static FileOutputStream allocateResource() { + try { + File file = new File("foo.txt"); + return new FileOutputStream(file); + } catch (IOException e) { + return null; + } + } + +}