From 97cb854e5ce80a297c215f0ed74987fba1bdf711 Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Thu, 17 Mar 2016 07:16:13 -0700 Subject: [PATCH] Model NSArray::arrayWithObject Summary:public This models [NSArray arrayWithObject:o] to require [o] to be non-nil, reporting NULL_DEREFERENCE otherwise. Reviewed By: jvillard Differential Revision: D2968563 fb-gh-sync-id: 04ae123 shipit-source-id: 04ae123 --- infer/models/objc/src/NSArray.m | 5 +++++ .../objc/errors/npe/nil_in_array_literal.m | 14 ++++++++++++++ infer/tests/endtoend/objc/NPEArrayLiteralTest.java | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/infer/models/objc/src/NSArray.m b/infer/models/objc/src/NSArray.m index c73bdb125..b36f3a03a 100644 --- a/infer/models/objc/src/NSArray.m +++ b/infer/models/objc/src/NSArray.m @@ -15,4 +15,9 @@ return [NSArray alloc]; } ++ (instancetype)arrayWithObject:(char*)anObject { + char _ = *anObject; + return [NSArray alloc]; +} + @end diff --git a/infer/tests/codetoanalyze/objc/errors/npe/nil_in_array_literal.m b/infer/tests/codetoanalyze/objc/errors/npe/nil_in_array_literal.m index 934af3287..d8422fcea 100644 --- a/infer/tests/codetoanalyze/objc/errors/npe/nil_in_array_literal.m +++ b/infer/tests/codetoanalyze/objc/errors/npe/nil_in_array_literal.m @@ -19,6 +19,10 @@ NSArray* foo = @[ @"aaa", @"bbb" ]; // check that array literals create valid objects NSArray* foofoo = @[ foo ]; + NSArray* bar = [NSArray arrayWithObject:@"ccc"]; + // test return value of arrayWithObject to avoid RETURN_VALUE_IGNORED report + if (bar) + return; } - (void)nilInArrayLiteral0 { @@ -49,11 +53,21 @@ NSArray* foo = @[ @"aaa", @"bbb", str ]; } +- (NSArray*)nilInArrayWithObject { + NSString* str = nil; + + // nil argument in arrayWithObject crashes + NSArray* foo = [NSArray arrayWithObject:str]; + + return foo; +} + @end int main() { A* a = [A alloc]; [a noProblem]; [a nilInArrayLiteral0]; + [a nilInArrayWithObject]; return 0; } diff --git a/infer/tests/endtoend/objc/NPEArrayLiteralTest.java b/infer/tests/endtoend/objc/NPEArrayLiteralTest.java index 356f6007c..b5d30d413 100644 --- a/infer/tests/endtoend/objc/NPEArrayLiteralTest.java +++ b/infer/tests/endtoend/objc/NPEArrayLiteralTest.java @@ -63,7 +63,8 @@ public class NPEArrayLiteralTest { "nilInArrayLiteral0", "nilInArrayLiteral1", "nilInArrayLiteral2", - "nilInArrayLiteral3" + "nilInArrayLiteral3", + "nilInArrayWithObject" }; assertThat( "Only NPE should be found", inferResults,