Mark Bernstein
Mark Bernstein

Reputation: 2080

XCode 4.6 clang analyzer worried over null C++ pointers than can't be null

Running the analyzer for XCode 4.6 and the new clang compiler, I seelots of warnings

Called C++ object pointer is null

even for guarded expressions like

- (BOOL) validate: (Node*) node
{
    if (!self.hypertext) return YES;
    return self.hypertext->HasNode(node);
}

1) How can I convince the static analyzer that this is properly guarded? 2) In some situations, I might want to assert that a pointer won't be null. How?

Upvotes: 2

Views: 3851

Answers (2)

Alex Reche Martinez
Alex Reche Martinez

Reputation: 966

The problem here is that your are not working with a pointer but with the result of a ObjC call.

Although very unlikely, the analyzer will see the following case: the pointer returned in the first call is not null and so the test passes, the pointer returned in the second call is null.

You should be able to solve the problem with a local variable.

- (BOOL) validate: (Node*) node
{
    YourType ht = [self hypertext];
    if (!ht) return YES;
    return ht->HasNode(node);
}

Upvotes: 7

bames53
bames53

Reputation: 88155

The clang static analyzer apparently supports a 'nonnull' attribute on functions for declaring that pointers are expected to not be null. I'm not sure about applying it to Obj-C methods instead though.

int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));

http://clang-analyzer.llvm.org/annotations.html#attr_nonnull

Upvotes: 1

Related Questions