Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglashill/cf9c3839f76db5ac5351e695fd493fd0 to your computer and use it in GitHub Desktop.
Save douglashill/cf9c3839f76db5ac5351e695fd493fd0 to your computer and use it in GitHub Desktop.
Sending a message to nil is supposed to do nothing in Objective-C, but here it’s changing the local variable error to nil. Tested with Xcode 12.5 on macOS 11.3.1.
NSError *error = [NSError errorWithDomain:@"domain" code:123 userInfo:nil];
[(NSFileHandle *)nil seekToOffset:0 error:&error];
assert(error != nil); // 💥 Not OK. The error has gone!
@mpw
Copy link

mpw commented May 24, 2021

Works correctly for me:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[]) {

	NSError *error = [NSError errorWithDomain:@"domain" code:123 userInfo:nil];
	[(NSFileHandle *)nil seekToOffset:0 error:&error];
	assert(error != nil); 
	printf("got here\n");
}

cc -o msgnil msgnil.m -framework Cocoa && ./msgnil
got here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment