Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: possible NULL && dereferencing NULL pointer |
| Date | 2012-01-31 00:28 -0800 |
| Organization | None to speak of |
| Message-ID | <lnliooxolh.fsf@nuthaus.mib.org> (permalink) |
| References | <jg7srm$ka8$1@speranza.aioe.org> <chine.bleu-B07EB1.21390330012012@news.eternal-september.org> |
Devil with the China Blue Dress <chine.bleu@yahoo.com> writes:
> In article <jg7srm$ka8$1@speranza.aioe.org>,
> "Mark" <mark_cruzNOTFORSPAM@hotmail.com> wrote:
>
>> 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:
>
> mem* and str* functions are generally undefined on null pointers. However it's
> easy enough to build your own function on top of these that do handle nulls:
>
> static inline int memorycmp(void *a, void *b, size_t n) {
> return a==b ? 0 : !a ? -1 : !b ? 1 : memcmp(a, b, n);
> }
>
> extends memcmp to nulls, with the additional relations NULL==NULL and
> NULL<nonnull. On the other hand if you want NULL=="",
>
> static inline int memorycmp(void *a, void *b, size_t n) {
> if (!a) a = "";
> if (!b) b = "";
> return memcmp(a, b, n);
> }
>
> 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.
--
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
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