possible NULL && dereferencing NULL pointer



Hello

Consider the snippet (this is from the code base I have to maintain):

#define NAME_LEN 10
struct foo
{
char name[NAME_LEN];
unsigned int time;
/* more other fields */
};

struct master
{
struct foo *f;
unisgned int flags;
struct hw_callbacks *hw_cb;
};

struct master *p_master = NULL;
....
/* allocate storage for struct master and assign ptr to p_master */
....

if (! p_master->f && memcmp (p_master->f->name, "test", NAME_LEN))
{
return ERR;
}

(and I have such construct all over the code)

I'm not sure that memcmp() will not fail if p_master->bridge is NULL (i.e.
dereferencing NULL pointer), would it be more clear and correct to have:

if (! p_master->f)
return ERR;

if (memcmp(p_master->f->name, "test", NAME_LEN))
return ERR;

Thanks.

Mark


.



Relevant Pages