Overlapped Buffers for string functions

Sankar P sankar.curiosity@gmail.com
Tue May 12 10:22:00 GMT 2009


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.

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.

Are there any other better methods that I am overlooking ?

I am not subscribed to the list, so please add my address in CC while
replying. Thanks.


More information about the Libc-help mailing list