Christopher Browne cbbrowne at ca.afilias.info
Thu Feb 11 12:37:41 PST 2010
Gwyn Connor <gwyn.connor at googlemail.com> writes:
> the slon_log function in 2.0.3-rc3 contains a very bad sprintf bug,
> where source and target buffer are the same. In such a case the
> standard says that the result of sprintf is undefined (according to
> the sprintf man page). And this causes slon to not log timestamps on
> my Gentoo machine.
>
> I am not a C coder, so I don't know the proper way of fixing it.

Here's what I suggest as a better way...

The trouble:
 - We keep "recursively" adding bits to outbuf

What we should do instead:
 - Generate "prefix chunks" individually
 - Add them all to outbuf in one fell swoop

Suggested patch, covering this all:

Index: misc.c
===================================================================
RCS file: /home/cvsd/slony1/slony1-engine/src/slon/misc.c,v
retrieving revision 1.26.2.3
diff -c -r1.26.2.3 misc.c
*** misc.c	9 Dec 2009 20:55:43 -0000	1.26.2.3
--- misc.c	11 Feb 2010 20:35:02 -0000
***************
*** 80,85 ****
--- 80,86 ----
  	char	   *level_c = NULL;
  
  	char		time_buf[128];
+ 	char        ps_buf[128];
  	time_t		stamp_time = time(NULL);
  
  	
***************
*** 173,185 ****
  			perror("slon_log: problem with strftime()");
  			slon_retry();
  		}
! 		sprintf(outbuf, "%s ", time_buf);
  	}
  	if (logpid == true)
  	{
! 		sprintf(outbuf, "%s[%d] ", outbuf, slon_pid);
  	}
! 	sprintf(outbuf, "%s%-6.6s ", outbuf, level_c);
  
  	off = (int) strlen(outbuf);
  
--- 174,191 ----
  			perror("slon_log: problem with strftime()");
  			slon_retry();
  		}
! 	} else {
! 		time_buf[0] = (char) 0;
  	}
+ 
  	if (logpid == true)
  	{
! 		sprintf(ps_buf, "[%d] ", slon_pid);
! 	} else {
! 		ps_buf[0] = (char) 0;
  	}
! 
! 	sprintf(outbuf, "%s%s%-6.6s ", time_buf, ps_buf, level_c);
  
  	off = (int) strlen(outbuf);
  

-- 
(format nil "~S@~S" "cbbrowne" "ca.afilias.info")
Christopher Browne
"Bother,"  said Pooh,  "Eeyore, ready  two photon  torpedoes  and lock
phasers on the Heffalump, Piglet, meet me in transporter room three"


More information about the Slony1-general mailing list