Search code examples
cc99coverity

'Pointer to local outside scope' by static analyzis -- false positive?


I have got an issue flagged by Coverity that I cannot understand.

I have an itializer:

1686  arrayOfNodeIds componentRefs = (arrayOfNodeIds) {
1687    .size = 2,
1688    .ids  = (UA_NodeId[]) { UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY)}
1689  };

member ids holds an array. Then this struct is given to a function:

1707  UA_Server_addInstanceOf_instatiateChildNode(server, &subtypeRefs, &componentRefs, &typedefRefs,
1708                                              objectRoot, callback, (UA_ObjectTypeNode *) typeDefNode, 
1709                                              UA_TRUE, &instantiatedTypes, handle);

the function dereferences conponentRefs->ids and Coverity flaggs this as access to a local variable outside of scope.

By googling i found a similar issue in one linux driver that was solved by using a memcpy to a stack variable. However, I do not understand the problem at all. Is the initializer of the internal array considered as a scope limiter? The problematic file can be found on github.

P.S.: definition of arrayOfNodeIds:

typedef struct arrayOfNodeIds_s {
  UA_Int32  size;
  UA_NodeId *ids;
} arrayOfNodeIds;

Solution

  • isd is a pointer and you have it point at a compound literal. All compound literals are to be regarded as local variables and they have local scope.

    So if your struct is of static storage duration (not really clear what you mean with "static itializer"), then the tool is right to complain. Because in that case, as soon as the program leaves the scope where you initialize isd, it will point at garbage. You'll have to point at another static storage duration variable or alternatively use dynamic allocation.