This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Overlapped Buffers for string functions


On Tue, May 12, 2009 at 03:52:24PM +0530, Sankar P wrote:
> Hi,
> 
> I have two questions.
> 
> In my attempt to become one of the Geeks, I was trying to fix a kernel
> bug - http://bugzilla.kernel.org/show_bug.cgi?id=12966
> 
> I came across the sprintf function, and I was told that the source and
> destination buffers should not overlap i.e.,
> 
> sprintf(buffer, "%s appended", buffer);
> 
> is a wrong usage and how it should be used is, like:
> 
> sprintf(buffer + strlen(buffer), " appended");
> 
> 
> 
> (1) Is this correct ?
> I could not find this information from the man pages.

Yes, that is correct. An appropriate note is in the man page, but it has
been added only very recently.

> I belive that appending to a string is a common usecase and a new api
> could be very handy in a lot of places.
> 
> saprintf = string append printf
> 
> char * buffer saprintf (char *buffer, const char * format, ...);
> 
> This function internally could do just,
> 
>                 sprintf (buffer + strlen (buffer), format, ...);
> 
> + plus other error checks
> 
> (2) Is it okay to implement this new api - saprintf ?  I am willing to
> give a patch, if the idea is approved.

I don't personally think a new API would be beneficial when this is so
trivially doable already.

-- 
				Petr "Pasky" Baudis
The lyf so short, the craft so long to lerne. -- Chaucer


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]