Clang Error on“Potential null dereference”。 [英] Clang Error on "Potential null dereference."
问题描述
我继续在以下类型的代码上得到Clang错误,我不知道为什么他们错误或如何解决他们Clang的满意:
I keep getting Clang errors on the following type of code and I can't figure out why they're erroneous or how to resolve them to Clang's satisfaction:
+ (NSString *)checkForLength: (NSString *)theString error: (NSError **)error {
BOOL hasLength = ([theString length] > 0);
if (hasLength) return theString;
else {
*error = [NSError errorWithDomain:@"ErrorDomain" code:hasLength userInfo:nil];
return nil;
}
}
省略示例的完全Clang反对,所以它是说明性的),Clang balks在错误分配行与以下反对:
Leaving aside the utterly-contrived nature of the example (which Clang did object to so it's illustrative enough), Clang balks at the error assignment line with the following objection:
潜在的空解引用。根据创建和返回
NSError
Objects中的编码标准,参数error可以为null。
Potential null dereference. According to coding standards in 'Creating and Returning
NSError
Objects' the parameter 'error' may be null.
我喜欢有一个原始的Clang报告。我读过引证文件,我看不到一个方法来做预期;我检查了一些开源的Cocoa库,这似乎是一个常见的成语。任何想法?
I like having a pristine Clang report. I've read the cited document and I can't see a way to do what's expected; I checked some open-source Cocoa libraries and this seems to be a common idiom. Any ideas?
推荐答案
执行预期的方式如清单3-5所示。你的示例代码:
The way to do what's expected is shown in listing 3-5 in that document. With your example code:
+ (NSString *)checkForLength: (NSString *)theString error: (NSError **)error {
BOOL hasLength = ([theString length] > 0);
if (hasLength) return theString;
else {
if (error != NULL) *error = [NSError errorWithDomain:@"ErrorDomain" code:hasLength userInfo:nil];
return nil;
}
}
这篇关于Clang Error on“Potential null dereference”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!