strdup(NULL) supposed to create SIGSEGV?
Garrett Cooper
yanefbsd at gmail.com
Sat Apr 26 06:34:22 UTC 2008
On Thu, Apr 24, 2008 at 1:24 AM, Bakul Shah <bakul at bitblocks.com> wrote:
> On Wed, 23 Apr 2008 23:34:41 PDT "Garrett Cooper" <yanefbsd at gmail.com>
> wrote:
> > I know that dereferencing a NULL pointer yields a segfault because
> address 0
> > can't be accessed.
> >
> > The point is that I didn't realize that something unadvertised causes a
> > SIGSEGV.
>
> I understood that. We all learn this the hard way! Reading
> that something will cause SIGSEGV doesn't have the same
> impact as actually seeing your program crash!
>
> > If someone noted that strdup(2) used memcpy(2) (at least) and didn't
> check
> > input (at most), I would completely understand.. I'm more than happy to
> > write that up in the revised manpage if that's what it takes, so others
> > unaware of this can avoid this issue..
>
> strdup(NULL) is an example of the more general case of using
> an illegal value for a function argument. Are you going to
> update all the manpages for functions that segfault when
> given a NULL? I guess what I am trying to say is don't waste
> your time on that -- even if you update all manpages, people
> are still going to trip over this at least once.
>
> cc -Wall can help find such problems. For strdup(NULL)
> you will get something like
>
> x.c:5: warning: null argument where non-null required (argument 1)
>
> So if you want to help others, make them use cc -Wall :-)
>
When using things such as getopt, which depend on user input, you can't
do that. gcc is good at mapping out graphs, but it's no more proficient of a
guesser than I am. Besides, it doesn't have a fake Jamaican accent :]..
I agree though to some extent about the manpage thing, even though this
stupid mistake bit me in the ass due to my own ignorance.
Also, strdup(3) should reference memcpy(3) in the manpage. I'll submit a
patch for that.
Discussion closed.
-Garrett
More information about the freebsd-hackers
mailing list