Re: Static analyzer making unwarranted assumptions about ivars
Re: Static analyzer making unwarranted assumptions about ivars
- Subject: Re: Static analyzer making unwarranted assumptions about ivars
- From: Devin Coughlin <email@hidden>
- Date: Tue, 15 Mar 2016 13:37:48 -0700
On Mar 15, 2016, at 12:14 PM, Jens Alfke < email@hidden> wrote:
On Mar 15, 2016, at 11:59 AM, Devin Coughlin < email@hidden> wrote:
One thing to note is that the analyzer doesn’t treat a parameter with a ‘nonnull’ type qualifier as definitely not nil.
It’s the other way around — I’m _calling_ a function (dispatch_async) with a nonnull parameter declaration, but the analyzer has decided the variable I’m passing might be nil, hence the warning. The code looks sort of like: if (!_queue) { _queue = _otherQueue; } dispatch_async(_queue, { … }); (Both _queue and _otherQueue are ivars typed as dispatch_queue_t.) I happen to know that _otherQueue is non-nil, because I assign it in the -init method and never modify it.
If you tell the analyzer about this invariant by asserting that _otherQueue is not nil in this method, the warning should go away. But the analyzer seems to be deciding it might be nil, and warns that I’m passing nil to dispatch_async.
This alone should not be enough to trigger an analyzer warning because “might be nil” is not the criteria; “must be nil” is. Is it possible there is another ‘if’ in the method that compares the value in _otherQueue to a value that is itself nil?
The analyzer doesn’t do any reasoning about potential interference from other threads.
Hm. The other inexplicable warning involves some tracing across method calls, and part of what has to go wrong is that a specific method returns nil. The method ends like: … // at this point _foo can’t be nil assert(_foo); NSLog(@“%@: foo is %@“, self, _foo); return _foo; I added the assertion to try to make the warning go away, but it doesn’t go away. However, if I move the assert call down to below the NSLog, the warning goes away. It looks as though the analyzer believes that calling NSLog can change _foo to nil somehow.
Assuming _foo is an instance variable on self, the call to NSLog() could potentially change _foo when self’s -description method is called. This is unlikely, but the analyzer heuristic is that when an instance escapes via a function or method call that it doesn’t have the source to (i.e., it can’t inline), then its instance variables could be modified and thus nothing should be assumed about their contents after the call.
Devin |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden