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,