Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16472

possible NULL && dereferencing NULL pointer

From "Mark" <mark_cruzNOTFORSPAM@hotmail.com>
Newsgroups comp.lang.c
Subject possible NULL && dereferencing NULL pointer
Date 2012-01-31 00:05 -0500
Organization Aioe.org NNTP Server
Message-ID <jg7srm$ka8$1@speranza.aioe.org> (permalink)

Show all headers | View raw


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 

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

possible NULL && dereferencing NULL pointer "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-01-31 00:05 -0500
  Re: possible NULL && dereferencing NULL pointer Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-30 21:39 -0800
    Re: possible NULL && dereferencing NULL pointer Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-31 08:25 +0000
    Re: possible NULL && dereferencing NULL pointer Keith Thompson <kst-u@mib.org> - 2012-01-31 00:28 -0800
      Re: possible NULL && dereferencing NULL pointer Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-31 00:33 -0800
        Re: possible NULL && dereferencing NULL pointer Keith Thompson <kst-u@mib.org> - 2012-01-31 02:31 -0800
          Re: possible NULL && dereferencing NULL pointer Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-01-31 04:54 -0800
      Re: possible NULL && dereferencing NULL pointer gwowen <gwowen@gmail.com> - 2012-01-31 02:30 -0800
        Re: possible NULL && dereferencing NULL pointer Keith Thompson <kst-u@mib.org> - 2012-01-31 13:20 -0800
  Re: possible NULL && dereferencing NULL pointer Robert Wessel <robertwessel2@yahoo.com> - 2012-01-31 01:12 -0600
  Re: possible NULL && dereferencing NULL pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 12:33 +0000
  Re: possible NULL && dereferencing NULL pointer jgk@panix.com (Joe keane) - 2012-02-01 15:15 +0000

csiph-web