On 2/17/11 1:23 AM, Guy Harris wrote:
> To correct this warning, examine the pointer for null value as shown in the following code:
>
> #include <malloc.h>
> void f( )
> {
> char *p = ( char * )malloc ( 10 );
> if ( p )
> {
> *p = '\0';
> // code ...
>
> free( p );
> }
> }
> "
>
> (not "if ( p != NULL )", just "if ( p )").
That doesn't produce any errors with "/analyze". The following tests
compile without errors as well:
--------
#include <malloc.h>
void f( )
{
char *p;
if (p = ( char * ) malloc( 10 )) {
*p = '\0';
free( p );
}
}
--------
--------
#include <malloc.h>
#include <stdlib.h>
void f( )
{
char *p;
int testval = 1;
if (((p = ( char * ) malloc( 10 )) != NULL) && testval) {
*p = '\0';
free( p );
}
}
--------
However this produces an error:
--------
#include <malloc.h>
void f( )
{
char *p;
int testval = 1;
if ((p = ( char * ) malloc( 10 )) && testval) {
*p = '\0';
free( p );
}
}
--------
test.c(9) : warning C6011: Dereferencing NULL pointer 'p': Lines: 5, 6, 8, 9
--
Join us for Sharkfest ’11! · Wireshark® Developer and User Conference
Stanford University, June 13-16 · http://sharkfest.wireshark.org