From: Shawn Ostermann (sdo@picard.cs.ohiou.edu)
Date: 01/05/06
Subject: Re: tcptrace-bugs Static variable returns of HostName() are not consider by callers Date: Thu, 05 Jan 2006 13:23:31 -0500 From: Shawn Ostermann <sdo@picard.cs.ohiou.edu> Message-Id: <20060105182331.728CFC53392@picard.cs.ohiou.edu>
Joshua Blanton <jblanton@masaka.cs.ohiou.edu> wrote:
> Shawn Ostermann wrote:
> > We should talk about this first. The extra overhead of the strdup()
> > plus the free() after the printf() will be expensive. And, of
> > course, somebody will forget to use free, or will have a custom bit
> > of code that doesn't use it, which will lead to a terrible memory
> > leak that will be very difficult to find.
>
> Hm, it seems to me that the suggested fix is sufficient... In the
> case of printf() (and any other function that would potentially
> re-use the pointer more than once in an instance) we need to do some
> clean-up - but in the case of printf(), the cleanup is trivial.
> Just split the line printed into multiple lines and it All Gets
> Better. Existing code is already safe from this bug, other than
> debug messages, so why not just fix the debug messages?
Well, it's a religious/stylistic question, I guess. I hate using
dynamic memory for these things. When I encounter these situations, I
normally just change the routine to rotate through static buffers and
then document that it does it. That way it can be used safely multiple
times in printf().
An example of that technique is plotter.c:xp_timestamp:
xp_timestamp()
{
#define NUM_BUFS 4
static char bufs[NUM_BUFS][20]; /* several of them for multiple calls in one printf */
static int bufix = 0;
[...]
/* use one of 4 rotating static buffers (for multiple calls per printf) */
bufix = (bufix+1)%NUM_BUFS;
pbuf = bufs[bufix];
snprintf(pbuf,sizeof(bufs[bufix]),"%u.%06u",secs,decimal);
return(pbuf);
}
I guess on second look, that would be my preferred solution.
Shawn
This archive was generated by hypermail 2.1.7 : 01/06/06 EST