Clang Error NSError** null dereference
I was using the Clang Static Analyzer tool for Objective C for Iphone developer , and when I saw the report , I notice several errors on the report , but it was the same NSError** null dereference in the report from ClangAfter seeing the detail information about the error, it said: Potential null dereference. According to coding standards in ‘Creating and Returning NSError Objects’ the parameter ‘error’ may be null.
Seeing some documentation in the apple website, I found that you need to check if the variable error is NULL or not because when you return a response to the caller if everything when fine , then you return the ERROR = NULL , and the error can be requested by the caller. So this problem will be fixed by just checking first if the error is not NULL then do what you have to do with the error.
- (BOOL)scanRestOfNull:(NSNull **)o error:(NSError **)error
{
if (!strncmp(c, "ull", 3)) {
c += 3;
*o = [NSNull null];
return YES;
}
if( error != nil)
{
*error = err(EPARSE, @"Expected 'null'");
}
return NO;
}
As you can see in the last part , first I check if the error is not NULL and if is not the I create an error , and can be retrieved by the caller. This simple validation will fix the problem found by the Clang Static Analyzer.
You can find more information in the apple site: http://developer.apple.com/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html#//apple_ref/doc/uid/TP40001806-CH204-BAJIIGCC%3E
May 18th, 2010 at 2:06 pm
Well noted! Clang produced the same error several times on SBJSON. Thanks for sharing.