This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Overlapped Buffers for string functions
- From: Sankar P <sankar dot curiosity at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Tue, 12 May 2009 15:52:24 +0530
- Subject: Overlapped Buffers for string functions
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.
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.
--
Sankar
http://psankar.blogspot.com