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


Groups > comp.lang.c > #16480

Re: possible NULL && dereferencing NULL pointer

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: possible NULL && dereferencing NULL pointer
Date 2012-01-31 02:31 -0800
Organization None to speak of
Message-ID <lnhazcxixc.fsf@nuthaus.mib.org> (permalink)
References <jg7srm$ka8$1@speranza.aioe.org> <chine.bleu-B07EB1.21390330012012@news.eternal-september.org> <lnliooxolh.fsf@nuthaus.mib.org> <chine.bleu-0465AF.00330331012012@news.eternal-september.org>

Show all headers | View raw


Devil with the China Blue Dress <chine.bleu@yahoo.com> writes:
> In article <lnliooxolh.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org> 
> wrote:
>> Devil with the China Blue Dress <chine.bleu@yahoo.com> writes:
[snip]
>> > You shouldn't be afraid of developping your own library on top of the std* 
>> > libraries to adapt them to your own notion of rightness.
>> 
>> Sure, but it's not clear to me that your memorycmp() is "right".
>> 
>> memcmp() compares the memory pointed to by one pointer to the memory
>> pointed to by another pointer.  A null pointer doesn't point to
>> anything, so I'd argue that if either pointer argument is null,
>> the comparison just doesn't make sense.  If you attempt to pass
>> one or two null pointers to memcmp(), it probably indicates a an
>> error in your program logic; hiding this error by quieting returning
>> a "sensible" value is not, IMHO, a good idea.
>> 
>> Mark, my advice is to fix your code's logic so it doesn't try to call
>> memcpy() with null arguments.
>
> Ah. So you're advocating programming to someone else's libraries that are 
> decades old rather than coding what your own intentions. Got it.

No.  I doubt that Mark's code, as posted, actually reflected his
intentions.

Here's the code in question:

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

It calls memcmp() *only* if p_master->f is a null pointer.
That's almost certainly a bug.  It might not be there in Mark's
actual code, but it's in what he posted.

I advocate using the standard library when it's appropriate -- not
because it's obviously superior to any alternative, but because it's
standard.

Perhaps Mark would be better off developing his own library, but I see
no evidence of it so far.

Oh, and you might want to read the rest of what I wrote.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | 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