possible NULL && dereferencing NULL pointer
- From: "Mark" <mark_cruzNOTFORSPAM@xxxxxxxxxxx>
- Date: Tue, 31 Jan 2012 00:05:54 -0500
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
.
- Follow-Ups:
- Re: possible NULL && dereferencing NULL pointer
- From: Ben Bacarisse
- Re: possible NULL && dereferencing NULL pointer
- From: Robert Wessel
- Re: possible NULL && dereferencing NULL pointer
- Prev by Date: Re: [I'm learning C]: Learning to use ucontext
- Next by Date: Re: JPEG to 2d array
- Previous by thread: JPEG to 2d array
- Next by thread: Re: possible NULL && dereferencing NULL pointer
- Index(es):
Relevant Pages
|