Why is Visual C++ 2010 complaining about 'Using uninitialized memory'?
I've got a function that takes a pointer to a buffer, and the siz开发者_JAVA技巧e of that buffer (via a pointer). If the buffer's not big enough, it returns an error value and sets the required length in the 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 like this:
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 code analysis cranked to the max) complains with 'warning C6001: Using uninitialized memory 'p': Lines: ...'
. It reports line numbers covering pretty much the entire function.
Visual C++ 2008 doesn't. As far as I can tell, this code's OK. What am I missing? Or what is VC2010 missing?
This has to be a bug in Visual Studio 2010. Wrapping malloc
removes the warning, as in the following tested code:
char * mymalloc(int i)
{
return (char *) malloc(i);
}
// ...
void *r = mymalloc(cb);
char *p;
p = (char *) malloc(cb);
You did not check if the first malloc() is ok. This leads to the warning.
精彩评论