llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False-positive with CLANG_ANALYZER_NONNULL

Open llvmbot opened this issue 10 years ago • 4 comments

Bugzilla Link 26143
Version unspecified
OS MacOS X
Attachments warning with code
Reporter LLVM Bugzilla Contributor
CC @devincoughlin

Extended Description

Summary: CLANG_ANALYZER_NONNULL with Xcode 7.3 Beta 1 is giving false-positive warnings.

Steps to Reproduce: static NSString *stringArray[] = { @"http://example.net" };

  • (void)load { [[[self alloc] init] useIndex:0]; }
  • (void)useIndex:(int)index { if (stringArray[index]) { } NSURL *url = [NSURL URLWithString:stringArray[index]]; NSLog(@"%@", url); }

Expected Results: No warning

Actual Results: Warning "Null passed to a callee that requires a non-null argument"

Version: Xcode Version 7.3 beta (7D111g) OS X beta 10.11.4 (15E27e)

llvmbot avatar Jan 14 '16 16:01 llvmbot

When we have static NSString * const stringArray[] = { @"" };, could analyzer reason that stringArray[0] is never nil?

llvmbot avatar Jan 15 '16 04:01 llvmbot

Reproduced. Thanks for the report!

This happens because the analyzer does not reason that stringArray[0] is not nil and so when it sees the if statement, it explores two paths: a spurious path one when stringArray[0] is definitely nil and a feasible when it is definitely not nil.

It is along the spurious path that the analyzer warns about nullability.

One potential workaround is to move the call to +URLWithString: inside the 'if'. Another workaround is to add assert(stringArray[index] != nil) before the call.

devincoughlin avatar Jan 14 '16 21:01 devincoughlin

Warning reproductible with those few lines:

static NSString * const stringArray[] = { @"http://example.net" };

  • (void)load { if (stringArray[0]) { } [NSURL URLWithString:stringArray[0]]; }

llvmbot avatar Jan 14 '16 16:01 llvmbot

assigned to @tkremenek

llvmbot avatar Jan 14 '16 16:01 llvmbot