Problem with snprintf()

Doug Wyatt
Mon Aug 7 08:12:00 GMT 2000


I posted this to the Cygwin list, but it occurs to me that it
might be more appropriate to post my question here.

I've run across differences in the behavior of snprintf() on
Cygwin 1.1.4, with respect to RH6.2 or HP-UX (using either gcc
or the HP ANSI cc). On the HP, the return value is -1 when the
string exceeds 'size', while on Linux and Cygwin the return
value is always the length of the string that is or would have
been written (minus the NUL).  That one doesn't really concern

    snprintf( char *target, size_t size, const char *format, ... )

The difference about which I have a question is that on Linux
and HP-UX, when the string exceeds 'size' then (size - 1) chars
plus a NUL are written, but on Cygwin 'size' chars are written
and a NUL is placed into target[size] - probably ill-advised
if 'target' is only allocated 'size' bytes.

Is the newlib version wrong?  It appears to be in light of this
excerpt from the RH6.2 snprintf man entry, also:

  Return value
  snprintf and  vsnprintf  do  not write  more than size bytes
  (including the trailing '\0'), and return -1 if the output was
  truncated due to this limit.  (Thus until glibc 2.0.6.  Since
  glibc 2.1 these functions follow the C99 standard and return the
  number of characters (excluding the trailing '\0') which would
  have been written to the final string if enough space had been

The problem arises in a configure file which wants (size - 1)
bytes in 'target'.

Is the newlib behavior a variant or a bug?

Doug Wyatt

The tail of my cygcheck -v -s:
   83k 2000/06/11 D:\cygwin-v11x\bin\cygitcl30.dll - os=4.0 img=1.0 sys=4.0
                  "cygitcl30.dll" v0.0 ts=2000/6/10 22:34
   35k 2000/06/11 D:\cygwin-v11x\bin\cygitk30.dll - os=4.0 img=1.0 sys=4.0
                  "cygitk30.dll" v0.0 ts=2000/6/10 22:34
  402k 2000/06/11 D:\cygwin-v11x\bin\cygtcl80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtcl80.dll" v0.0 ts=2000/6/10 22:30
    5k 2000/06/11 D:\cygwin-v11x\bin\cygtclpip80.dll - os=4.0 img=1.0 sys=4.0
   10k 2000/06/11 D:\cygwin-v11x\bin\cygtclreg80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtclreg80.dll" v0.0 ts=2000/6/10 22:30
  639k 2000/06/11 D:\cygwin-v11x\bin\cygtk80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtk80.dll" v0.0 ts=2000/6/10 22:34
  586k 2000/08/04 D:\cygwin-v11x\bin\cygwin1.dll - os=4.0 img=1.0 sys=4.0
                  "cygwin1.dll" v0.0 ts=2000/8/3 19:53
    Cygwin DLL version info:
        dll major: 1001
        dll minor: 4
        dll epoch: 19
        dll bad signal mask: 19005
        dll old termios: 5
        api major: 0
        api minor: 26
        shared data: 3
        dll identifier: cygwin1
        mount registry: 2
        cygnus registry name: Cygnus Solutions
        cygwin registry name: Cygwin
        program options name: Program Options
        cygwin mount registry name: mounts v2
        build date: Thu Aug 3 20:53:46 EDT 2000
        CVS tag: cygwin-1-1-4
        shared id: cygwin1S3

More information about the Newlib mailing list