Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* xfree() -- set ptr to nil (fwd)
@ 2001-02-12 15:07 John R. Moore
  2001-02-12 15:21 ` Kevin Buettner
  2001-02-12 15:25 ` J.T. Conklin
  0 siblings, 2 replies; 9+ messages in thread
From: John R. Moore @ 2001-02-12 15:07 UTC (permalink / raw)
  To: gdb-patches

Whilst fixing xfree() callsI noticed that xfree() itself has a peculiarity
that needs attention:

The call goes like this:

if (ptr != NULL)
  free(ptr);

Nice, but why not the following:

if (ptr)
  {
     free (ptr);
     prt = NULL);
  }

The latter catches any re-calls to xfree(), unless the compiler sets the
ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
to do this anyhow.

Any opinions?  The only reason I can think not to is to insure that gdb
core dumps on succesive xfree() calls to the same pointer (and hence
insure efficient code, but in that case, why bother with xfree() in the
first place.

John Moore



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:07 xfree() -- set ptr to nil (fwd) John R. Moore
@ 2001-02-12 15:21 ` Kevin Buettner
  2001-02-12 15:33   ` John R. Moore
  2001-02-12 15:25 ` J.T. Conklin
  1 sibling, 1 reply; 9+ messages in thread
From: Kevin Buettner @ 2001-02-12 15:21 UTC (permalink / raw)
  To: John R. Moore, gdb-patches

On Feb 12,  3:07pm, John R. Moore wrote:

> Whilst fixing xfree() callsI noticed that xfree() itself has a peculiarity
> that needs attention:
> 
> The call goes like this:
> 
> if (ptr != NULL)
>   free(ptr);
> 
> Nice, but why not the following:
> 
> if (ptr)
>   {
>      free (ptr);
>      prt = NULL);
>   }
> 
> The latter catches any re-calls to xfree(), unless the compiler sets the
> ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
> to do this anyhow.
> 
> Any opinions?  The only reason I can think not to is to insure that gdb
> core dumps on succesive xfree() calls to the same pointer (and hence
> insure efficient code, but in that case, why bother with xfree() in the
> first place.

Let me see if I understand you correctly.  You'd like to replace

    void
    xfree (void *ptr)
    {
      if (ptr != NULL)
	free (ptr);
    }

with

    void
    xfree (void *ptr)
    {
      if (ptr)
	{
	  free (ptr);
	  ptr = NULL;
	}
    }

right?

If so, how will this work?  ``ptr'' is a local variable and will not
be modified outside the scope of xfree().

What you have in mind could be done with a macro and I have seen
this done in other programs.  (But rather than insuring that gdb
core dumps on successive xfree() calls, it instead causes gdb to
core dump when attempting to use an already freed-and-nulled pointer.)

Kevin


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:07 xfree() -- set ptr to nil (fwd) John R. Moore
  2001-02-12 15:21 ` Kevin Buettner
@ 2001-02-12 15:25 ` J.T. Conklin
  1 sibling, 0 replies; 9+ messages in thread
From: J.T. Conklin @ 2001-02-12 15:25 UTC (permalink / raw)
  To: John R. Moore; +Cc: gdb-patches

>>>>> "John" == John R Moore <jmoore@cygnus.com> writes:
John> Nice, but why not the following:
John>
John> if (ptr)
John>   {
John>      free (ptr);
John>      prt = NULL);
John>   }
John>
John> The latter catches any re-calls to xfree(), unless the compiler sets the
John> ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
John> to do this anyhow.
John>
John> Any opinions?  The only reason I can think not to is to insure that gdb
John> core dumps on succesive xfree() calls to the same pointer (and hence
John> insure efficient code, but in that case, why bother with xfree() in the
John> first place.

This won't work.  Remember ptr is passed by value.  While you can
change ptr's value within xfree(), that doesn't change the value of
the variable that was passed to it.

IMO, xfree()'s reason for existance is not protecting against multiple
frees of the same object, but supporting frees of a possibly NULL ptr.
Modern free() implementations already support this, but we support old
pre-ANSI C libraries as well.  

        --jtc

-- 
J.T. Conklin
RedBack Networks


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:21 ` Kevin Buettner
@ 2001-02-12 15:33   ` John R. Moore
  2001-02-12 15:46     ` Daniel Berlin
  2001-02-12 15:54     ` J.T. Conklin
  0 siblings, 2 replies; 9+ messages in thread
From: John R. Moore @ 2001-02-12 15:33 UTC (permalink / raw)
  To: Kevin Buettner; +Cc: gdb-patches

Yes, I've usually seen this as a macro

#define XFREE(ptr) do \
{ \
 if (ptr) \
  { \
    free (ptr); \
    ptr = NULL; \
  } \
} while (0)

Ok, then, do we want to replace xfree() with something like XFREE() ?

John   (Can I hide my first post? :-)  )


On Mon, 12 Feb 2001, Kevin Buettner wrote:

> On Feb 12,  3:07pm, John R. Moore wrote:
>
> > Whilst fixing xfree() callsI noticed that xfree() itself has a peculiarity
> > that needs attention:
> >
> > The call goes like this:
> >
> > if (ptr != NULL)
> >   free(ptr);
> >
> > Nice, but why not the following:
> >
> > if (ptr)
> >   {
> >      free (ptr);
> >      prt = NULL);
> >   }
> >
> > The latter catches any re-calls to xfree(), unless the compiler sets the
> > ptr to nil for one (gcc doesn't appear to). Anyhow, it's a good practice
> > to do this anyhow.
> >
> > Any opinions?  The only reason I can think not to is to insure that gdb
> > core dumps on succesive xfree() calls to the same pointer (and hence
> > insure efficient code, but in that case, why bother with xfree() in the
> > first place.
>
> Let me see if I understand you correctly.  You'd like to replace
>
>     void
>     xfree (void *ptr)
>     {
>       if (ptr != NULL)
> 	free (ptr);
>     }
>
> with
>
>     void
>     xfree (void *ptr)
>     {
>       if (ptr)
> 	{
> 	  free (ptr);
> 	  ptr = NULL;
> 	}
>     }
>
> right?
>
> If so, how will this work?  ``ptr'' is a local variable and will not
> be modified outside the scope of xfree().
>
> What you have in mind could be done with a macro and I have seen
> this done in other programs.  (But rather than insuring that gdb
> core dumps on successive xfree() calls, it instead causes gdb to
> core dump when attempting to use an already freed-and-nulled pointer.)
>
> Kevin
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:33   ` John R. Moore
@ 2001-02-12 15:46     ` Daniel Berlin
  2001-02-12 15:52       ` John R. Moore
  2001-02-12 15:54     ` J.T. Conklin
  1 sibling, 1 reply; 9+ messages in thread
From: Daniel Berlin @ 2001-02-12 15:46 UTC (permalink / raw)
  To: John R. Moore; +Cc: Kevin Buettner, gdb-patches

On Mon, 12 Feb 2001, John R. Moore wrote:

>
> Yes, I've usually seen this as a macro
>
> #define XFREE(ptr) do \
> { \
>  if (ptr) \
>   { \
>     free (ptr); \
>     ptr = NULL; \
>   } \
> } while (0)
>
> Ok, then, do we want to replace xfree() with something like XFREE() ?

No, we try to get *rid* of macros, not add them.
--Dan


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:46     ` Daniel Berlin
@ 2001-02-12 15:52       ` John R. Moore
  0 siblings, 0 replies; 9+ messages in thread
From: John R. Moore @ 2001-02-12 15:52 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: Kevin Buettner, gdb-patches

Ok, I see the light. Thanks for responding quickly.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 15:33   ` John R. Moore
  2001-02-12 15:46     ` Daniel Berlin
@ 2001-02-12 15:54     ` J.T. Conklin
  1 sibling, 0 replies; 9+ messages in thread
From: J.T. Conklin @ 2001-02-12 15:54 UTC (permalink / raw)
  To: John R. Moore; +Cc: Kevin Buettner, gdb-patches

>>>>> "John" == John R Moore <jmoore@cygnus.com> writes:
John> Yes, I've usually seen this as a macro
John>
John> #define XFREE(ptr) do \
John> { \
John>  if (ptr) \
John>   { \
John>     free (ptr); \
John>     ptr = NULL; \
John>   } \
John> } while (0)
John>
John> Ok, then, do we want to replace xfree() with something like XFREE() ?

I'd say no, unless it has been established that we have a problem
where pointers are freed multiple times AND it's been determined that
its too difficult to fix the code to avoid the bug.

        --jtc

-- 
J.T. Conklin
RedBack Networks


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
  2001-02-12 16:01 Michael Elizabeth Chastain
@ 2001-02-13 14:12 ` Andrew Cagney
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Cagney @ 2001-02-13 14:12 UTC (permalink / raw)
  To: Michael Elizabeth Chastain; +Cc: jmoore, kevinb, gdb-patches

Some platforms dump core from free(NULL), yet an ISO-C free(NULL)
doesn't.  xfree() addresses this inconsistency by ensuring ISO-C
behavour.

	enjoy,
		Andrew

PS: Drugs^D^D^D^D^D Macros are bad M'kay ....


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: xfree() -- set ptr to nil (fwd)
@ 2001-02-12 16:01 Michael Elizabeth Chastain
  2001-02-13 14:12 ` Andrew Cagney
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Elizabeth Chastain @ 2001-02-12 16:01 UTC (permalink / raw)
  To: jmoore, kevinb; +Cc: gdb-patches

John Moore writes:

> Ok, then, do we want to replace xfree() with something like XFREE() ?

This gets into a philosophical discussion.  Here is my philosophy.
Others may differ.

Basically, I am interested in work that leads to patches that get applied
and have concrete, observable gains.  If someone can identify specific
bugs that we've had in gdb as a result of xfree problems, then I would
be interested in safety work such as s/xfree/XFREE/g, arranging to run
gdb regularly under Purify, and projects like that.

But I haven't seen problems like that so s/xfree/XFREE/g does not appeal
to me.

This is just a guideline.  I really liked Kevin Buettner's PARAMS removal
patches, for example.  In that case, the code afterwards was uneqivocally
simpler than the code before.

My two cents.  And I'm not a maintainer, just a write-after-approval guy.

Michael Elizabeth Chastain
<chastain@redhat.com>
"love without fear"


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2001-02-13 14:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-12 15:07 xfree() -- set ptr to nil (fwd) John R. Moore
2001-02-12 15:21 ` Kevin Buettner
2001-02-12 15:33   ` John R. Moore
2001-02-12 15:46     ` Daniel Berlin
2001-02-12 15:52       ` John R. Moore
2001-02-12 15:54     ` J.T. Conklin
2001-02-12 15:25 ` J.T. Conklin
2001-02-12 16:01 Michael Elizabeth Chastain
2001-02-13 14:12 ` Andrew Cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox