gregmb -> RE: sprintf memory leak (Sep. 19, '06, 11:02:10 AM) |
The code goes something like this.
staring point:
updateRfMon(&statblock->AcqShortRfLimit,
&CurrentStatBlock.AcqShortRfLimit,
'L', 4, "W",
RF10s1LimVal + i * 4);
Where statblock->AcqShortRfLimit & CurrentStatBlock.AcqShortRfLimit are unsigned long
RF10s1LimVal is just a defined integer #define RF10s1LimVal 10
static void
updateRfMon(unsigned long *newval,
unsigned long *oldval,
char justify,
int width,
char *unitstr,
int str_num)
{
if (*oldval != *newval) {
char buf[20];
*oldval = *newval;
power_format(buf, *newval, justify, width, unitstr);
disp_string(str_num, buf);
}
}
This is the routine that has the sprintf call, as you can see I tried ifdef out the
one sprintf() call since I thought maybe the one syntax wasn't handled by interix, but
even the simplified sprintf() still had the leak.
Just commenting out just the sprintf() lines stopped the leak.
static void
power_format(char *buf, long uwatts, char justify, int width, char *units)
{
float watts;
/* fprintf(stderr,"buf= 0x%lx, uwatts: %ld, just: '%c', width: %d, units: 0x%x\n",
buf,uwatts,justify,width,units); */
watts = uwatts * 1e-6;
if (watts < 99.9) {
switch (justify) {
case 'r': case 'R':
#ifndef __INTERIX /* this format causes interix to leak memory */
sprintf(buf,"%*.1f", width, watts);
#else
sprintf(buf,"%4.1f", watts); /* this too leaks memory */
#endif
break;
case 'l': case 'L':
sprintf(buf,"%.1f", watts);
break;
}
} else {
switch (justify) {
case 'r': case 'R':
#ifndef __INTERIX /* this format causes interix to leak memory */
sprintf(buf,"%*d", width, (int)(watts + 0.5));
#else
sprintf(buf,"%4d", (int)(watts + 0.5)); /* this too leaks memory */
#endif
break;
case 'l': case 'L':
sprintf(buf,"%d", (int)(watts + 0.5));
break;
}
}
if (units && *units) {
strcat(buf, " ");
strcat(buf, units);
}
}
It a bit convoluted for historical reasons, but has worked on Linux and Solaris for years.
Greg |
|
|
|