Static analysis question

I want to help a coworker in tracking down a spurious crash by using the Statically Analyze commmand in Visual Studio 2005, but I decided to give it a test drive first.

On this test, I expected it to report a use of an uninitialized variable.  Is this correct expectation of the /analyze switch   I'm using VS 2005 B2.

void foo( int& x )
{
   printf(
"%d", x );
}

int main()
{
   int uninitialized;
   foo( uninitialized );
  
return 0;
}



Answer this question

Static analysis question

  • Learner.example

    I seen your question on the other forum; a bit repeating here: code analysis is done per function.
    In case of passing parameter by-value it is reasonable to expect initialized variable as an argument on the caller side. This is not true in case of reference. So in this example code analysis gives a warning with a better confidence that it's not noisy.

    Thanks, 
    Natalia   



  • Mario Esposito - MSFT

    In order to minimize noise, code analysis doesn't make any assumptions about whether the callee requires an initialized variable. If the callee handled this, you can imagine the consternation to receive a warning on every call site to it.

    Try the following:



    #include <CodeAnalysis/SourceAnnotations.h>
    using namespace vc_attributes;

    void foo([Pre ( Valid = Yes )] int& x )
    {
       printf( "%d", x );
    }

    int main()
    {
       int uninitialized;
       foo( uninitialized );
       return 0;
    }

     



    See the following for more info on this topic:

    http://msdn2.microsoft.com/en-us/library/ms182032(en-US,VS.80).aspx



  • MEindert

    Then why does changing the call to pass by-value rather than by-ref give you a prefast diagnostic (Ayman pointed this out).

    #include <stdio.h>
    void foo( int x )
    {
       printf( "%d", x );
    }
    int main()
    {
       int uninitialized;
       foo( uninitialized );
       return 0;
    }

    If code analysis can detect the use of an uninitialized variable, it should report it.   Both versions are the same in that there is a use of an unintialized variable.

    Brian


  • Static analysis question