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()


Hi!

Monday, 07 August, 2000 Jonathan Larmour jlarmour@redhat.co.uk wrote:

>> 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 ?

JL> Here's what C99 says:

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

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

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

here's the patch to achieve this

Index: newlib/libc/stdio/snprintf.c
===================================================================
RCS file: /home/duda_admin/cvs-mirror/src/newlib/libc/stdio/snprintf.c,v
retrieving revision 1.1.1.1
diff -c -2 -r1.1.1.1 snprintf.c
*** newlib/libc/stdio/snprintf.c        2000/02/17 19:39:47     1.1.1.1
--- newlib/libc/stdio/snprintf.c        2000/08/07 15:45:11
***************
*** 80,84 ****
    f._flags = __SWR | __SSTR;
    f._bf._base = f._p = (unsigned char *) str;
!   f._bf._size = f._w = size;
    f._data = _REENT;
  #ifdef _HAVE_STDC
--- 80,84 ----
    f._flags = __SWR | __SSTR;
    f._bf._base = f._p = (unsigned char *) str;
!   f._bf._size = f._w = size - 1;
    f._data = _REENT;
  #ifdef _HAVE_STDC
Index: newlib/libc/stdio/vsnprintf.c
===================================================================
RCS file: /home/duda_admin/cvs-mirror/src/newlib/libc/stdio/vsnprintf.c,v
retrieving revision 1.1.1.1
diff -c -2 -r1.1.1.1 vsnprintf.c
*** newlib/libc/stdio/vsnprintf.c       2000/02/17 19:39:47     1.1.1.1
--- newlib/libc/stdio/vsnprintf.c       2000/08/07 15:45:11
***************
*** 46,50 ****
    f._flags = __SWR | __SSTR;
    f._bf._base = f._p = (unsigned char *) str;
!   f._bf._size = f._w = size;
    f._data = _REENT;
    ret = vfprintf (&f, fmt, ap);
--- 46,50 ----
    f._flags = __SWR | __SSTR;
    f._bf._base = f._p = (unsigned char *) str;
!   f._bf._size = f._w = size - 1;
    f._data = _REENT;
    ret = vfprintf (&f, fmt, ap);


Egor.            mailto:deo@logos-m.ru ICQ 5165414 FidoNet 2:5020/496.19



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