Why does Visual C ++ 2010 complain about "Using uninitialized memory"?

I have a function that takes a pointer to a buffer and the size of this buffer (via a pointer). If the buffer is not large enough, it returns an error value and sets the required length in out-param:

// FillBuffer is defined in another compilation unit (OBJ file).
// Whole program optimization is off.
int FillBuffer(__int_bcount_opt(*pcb) char *buffer, size_t *pcb);

I call it this way:

size_t cb = 12;
char *p = (char *)malloc(cb);
if (!p)
    return ENOMEM;

int result;
for (;;)
{
    result = FillBuffer(p, &cb);
    if (result == ENOBUFS)
    {
        char *q = (char *)realloc(p, cb);
        if (!q)
        {
            free(p);
            return ENOMEM;
        }

        p = q;
    }
    else
        break;
}

Visual C ++ 2010 (with maximum code analysis) complains about 'warning C6001: Using uninitialized memory 'p': Lines: ...'. It reports line numbers covering almost the entire function.

Visual C ++ 2008 no. As far as I can tell, this code is OK. What am I missing? Or what's missing VC2010?

+3
source share
2 answers

​​Visual Studio 2010. Wrapping malloc , :

char * mymalloc(int i)  
{  
    return (char *) malloc(i);  
}

// ...

void *r = mymalloc(cb);

char *p;

p = (char *) malloc(cb);
+3

, malloc(). .

-1

Source: https://habr.com/ru/post/1728277/


All Articles