Coverity. Configure coverity to check that a declared variable is initialized before usage as a pointer

675 Views Asked by At

Looking for a way to configure Coverity such that it will ensure that a declared variable on the stack is initialised prior to its address being passed to another function

For example in the code below x is declared on the stack, but it is not initialised and it is therefore indeterminate. The address of x is then passed to func2. Since the value of x is not defined, the behavior of func2 cannot be certain.

Can Coverity issue a warning for this type of error?

void func1(uint32_t* val)
{
    uint32_t x; /*x is not initialised!! */
    func2(val, &x);
}

void func2(uint32_t* val, uint32_t* x)
{
    uint32_t y;
    y = (*x) + (*v);
}
2

There are 2 best solutions below

5
On

Strange you don't have it, but having UNINIT checker enabled should do the trick.

Check how you execute cov-analyze. You can specify your own checkers configuration there by --dc-config parameter.

0
On

Thanks for the help on this one. The only real solution in this particular case is to ensure that the object is initialised prior to passing its address to a function. i.e. x = 0 in func1 above