This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


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

Re: Problem with snprintf()


Egor Duda wrote:
> 
> Hi!
> 
> Monday, 07 August, 2000 Corinna Vinschen vinschen@cygnus.com wrote:
> CV> I have found another problem with snprintf. While porting OpenSSH
> CV> to Cygwin I found that configure reports the following:
> 
> CV> configure:2567: checking whether snprintf correctly terminates long
> CV>                 strings
> CV> configure:      warning: ****** Your snprintf() function is broken,
> CV>                 complain to your vendor
> 
> CV> The configure file contains the following test:
> 
> CV> #include <stdio.h>
> CV> int main(void){char b[5];snprintf(b,5,"123456789");return(b[4]!='\0');}
> 
> CV> As you can see, it expects snprintf terminating strings with \0 always
> CV> which seems not to be the case for the newlib version.
> 
> CV> Shouldn't this be fixed as well?
> 
> Susv2  is not very informative on this subject. Does anybody have ANSI
> standard to make snprintf ANSI-compliant ?

Here's what C99 says:

-=-=-=-=-
The snprintf function is equivalent to fprintf, except that the output is
written into an array (specified by argument s) rather than to a stream. If
n is zero, nothing is written, and s may be a null pointer. Otherwise,
output characters beyond the n-1st are discarded rather than being written
to the array, and a null character is written at the end of the characters
actually written into the array. If copying takes place between objects
that overlap, the behavior is undefined.

Returns
The snprintf function returns the number of characters that would have been
written had n been sufficiently large, not counting the terminating null
character, or a negative value if an encoding error occurred. Thus, the
null-terminated output has been completely written if and only if the
returned value is nonnegative and less than n.
-=-=-=-=-

So snprintf() should always add a terminating \0.

Jifl
-- 
Red Hat, 35 Cambridge Place, Cambridge, UK. CB2 1NS  Tel: +44 (1223) 728762
"Plan to be spontaneous tomorrow."  ||  These opinions are all my own fault

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