From 2228f7448d69200e9494c333f7fd4bb8d7b538c6 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Mon, 5 Feb 2018 12:20:49 -0800 Subject: [PATCH] [infer][nullable checker] add tests for the early return Reviewed By: sblackshear Differential Revision: D6891113 fbshipit-source-id: a614641 --- infer/tests/codetoanalyze/cpp/nullable/issues.exp | 2 ++ infer/tests/codetoanalyze/cpp/nullable/method.cpp | 7 +++++++ .../codetoanalyze/java/checkers/NullableViolation.java | 7 +++++++ infer/tests/codetoanalyze/objc/nullable/Examples.m | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/infer/tests/codetoanalyze/cpp/nullable/issues.exp b/infer/tests/codetoanalyze/cpp/nullable/issues.exp index f93a740c7..f7197d9b2 100644 --- a/infer/tests/codetoanalyze/cpp/nullable/issues.exp +++ b/infer/tests/codetoanalyze/cpp/nullable/issues.exp @@ -20,6 +20,8 @@ codetoanalyze/cpp/nullable/method.cpp, methodAlwaysCheckedForNullOkay, 1, NULL_T codetoanalyze/cpp/nullable/method.cpp, methodAlwaysCheckedForNullOkay, 2, NULL_DEREFERENCE, [start of procedure methodAlwaysCheckedForNullOkay(),Condition is true,start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is true,start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject] codetoanalyze/cpp/nullable/method.cpp, methodCallOnFieldOfNullableObjectBad, 2, NULLABLE_DEREFERENCE, [dereference of &p,assignment of the nullable value,definition of mayReturnNullObject] codetoanalyze/cpp/nullable/method.cpp, methodCallOnFieldOfNullableObjectBad, 2, NULL_DEREFERENCE, [start of procedure methodCallOnFieldOfNullableObjectBad(),start of procedure mayReturnNullObject,Condition is true,return from a call to T_mayReturnNullObject] +codetoanalyze/cpp/nullable/method.cpp, methodCheckedForNullAndReturnOkay, 1, NULL_TEST_AFTER_DEREFERENCE, [start of procedure methodCheckedForNullAndReturnOkay(),start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is false] +codetoanalyze/cpp/nullable/method.cpp, methodCheckedForNullAndReturnOkay, 4, NULL_DEREFERENCE, [start of procedure methodCheckedForNullAndReturnOkay(),start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is false,start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject] codetoanalyze/cpp/nullable/method.cpp, methodCheckedForNullOkay, 1, NULL_TEST_AFTER_DEREFERENCE, [start of procedure methodCheckedForNullOkay(),start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is false] codetoanalyze/cpp/nullable/method.cpp, methodCheckedForNullOkay, 2, NULL_DEREFERENCE, [start of procedure methodCheckedForNullOkay(),start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is true,start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject] codetoanalyze/cpp/nullable/method.cpp, methodNotAlwaysCheckedForNullBad, 1, NULL_TEST_AFTER_DEREFERENCE, [start of procedure methodNotAlwaysCheckedForNullBad(),Condition is false,start of procedure mayReturnNullObject,Condition is false,return from a call to T_mayReturnNullObject,Condition is false] diff --git a/infer/tests/codetoanalyze/cpp/nullable/method.cpp b/infer/tests/codetoanalyze/cpp/nullable/method.cpp index 2aef93620..a582ee757 100644 --- a/infer/tests/codetoanalyze/cpp/nullable/method.cpp +++ b/infer/tests/codetoanalyze/cpp/nullable/method.cpp @@ -97,6 +97,13 @@ void methodCheckedForNullOkay(T* t) { } } +void methodCheckedForNullAndReturnOkay(T* t) { + if (t->mayReturnNullObject() == nullptr) { + return; + } + t->mayReturnNullObject()->doSomething(); // does not report here +} + void reportsViolationOutsideOfNullCheckBad(T* t) { if (t->mayReturnNullObject() != nullptr) { t->mayReturnNullObject()->doSomething(); // does not report here diff --git a/infer/tests/codetoanalyze/java/checkers/NullableViolation.java b/infer/tests/codetoanalyze/java/checkers/NullableViolation.java index 403e3feb4..991bfa2d5 100644 --- a/infer/tests/codetoanalyze/java/checkers/NullableViolation.java +++ b/infer/tests/codetoanalyze/java/checkers/NullableViolation.java @@ -43,6 +43,13 @@ public class NullableViolation { } } + void nullableMethodCheckedForNullAndReturnOkay() { + if (returnsNullable() == null) { + return; + } + returnsNullable().doSomething(); // does not report here + } + void dereferenceNullableMethodIncorrectlyCheckedForNullBad() { if (returnsNullable() == null) { returnsNullable().doSomething(); // reports here diff --git a/infer/tests/codetoanalyze/objc/nullable/Examples.m b/infer/tests/codetoanalyze/objc/nullable/Examples.m index 4c7bb9ab7..eed1acee9 100644 --- a/infer/tests/codetoanalyze/objc/nullable/Examples.m +++ b/infer/tests/codetoanalyze/objc/nullable/Examples.m @@ -129,6 +129,16 @@ typedef struct s_ { return array; } +- (NSArray*)nullableMethodCheckedForNullAndReturnOkay { + NSObject* nullableObject = [self nullableMethod]; + NSArray* array; + if (nullableObject == nil) { + return array; + } + array = @[ nullableObject ]; // does not report here + return array; +} + - (NSArray*)secondElementNullableObjectInNSArrayBad { NSObject* allocatedObject = [NSObject alloc]; NSObject* nullableObject = [self nullableMethod];