Issue 79333
Summary clang warns that completion is called twice but that can be ruled out through static code analysis
Labels clang
Assignees
Reporter CodingMarkus
    clang says that completion may be called twice in the following code:

```
- (void)readPacket:
	(void (^)( NSData *_Nullable, NSError *_Nullable))completion
{
	NSData * packet = nil;
	NSError * error = nil;

	[self obtainLock];
	{
		if (_pendingCallback) {
			error = [self makePendingError];
		} else {
			packet = [self getPacket];
			if (packet) {
				if (_asyncCompletion) {
					dispatch_async(_queue, ^{
						completion(packet, nil);
					});
					packet = nil;
				}
			} else {
				_pendingCallback = completion;
			}
		}
	}
	[self releaseLock];

	if (packet || error) {
		completion(packet, error); // <-- Here clang says: "Completion handler is called twice"
	}
}
```

I don't see how this would be the case. Completion is only called in the end, if either `packet` or `error` is set. Completion may only be called earlier when it should finish async but in that case `packet` is explicitly set back to `nil`, so it won't be called again in the end. Or it may be called later, when it is stored to `_pendingCallback`, but that only happens if `packet` and `error` are both `nil`, so there is for sure no call in the end either. When either `packet` or `error` are set in the end, completion was for sure never called before, never stored anywhere and never passed to any external function.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to