Sender: richdawe AT bigfoot DOT com Message-ID: <3A171C46.32871211@bigfoot.com> Date: Sun, 19 Nov 2000 00:18:14 +0000 From: Richard Dawe X-Mailer: Mozilla 4.51 [en] (X11; I; Linux 2.2.17 i586) X-Accept-Language: de,fr MIME-Version: 1.0 To: DJGPP workers Subject: snprintf() diff, take 4? Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp-workers AT delorie DOT com Hello. Here's the latest snprintf() diff. Changes: * Correct texinfo, as directed by Eli. * Extended tests, as suggested by Eli. I'm not sure how to induce a encoding error in printf() though. Formats like '%!' result in '!' as output. I've renamed the test t-snprtf.c from snprintf.c, for debugging with gdb. * snprintf() is now just a wrapper for vsnprintf(). * n == 0 handled properly now. The buffer pointer in FILE is set to NULL in this case, to catch bogus buffer accesses. I've left the "buffer full" logic in _flsbuf(). It can be changed easily enough. Bye, Rich *** /develop/djgpp/include/stdio.h Sun Nov 15 13:37:28 1998 --- /develop/djgpp.dev/include/stdio.h Sun Nov 12 21:18:46 2000 *************** *** 1,3 **** --- 1,4 ---- + /* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ *************** *** 94,99 **** --- 95,101 ---- int scanf(const char *_format, ...); void setbuf(FILE *_stream, char *_buf); int setvbuf(FILE *_stream, char *_buf, int _mode, size_t _size); + int snprintf(char *str, size_t n, const char *fmt, ...); int sprintf(char *_s, const char *_format, ...); int sscanf(const char *_s, const char *_format, ...); FILE * tmpfile(void); *************** *** 101,106 **** --- 103,109 ---- int ungetc(int _c, FILE *_stream); int vfprintf(FILE *_stream, const char *_format, va_list _ap); int vprintf(const char *_format, va_list _ap); + int vsnprintf(char *str, size_t n, const char *fmt, va_list ap); int vsprintf(char *_s, const char *_format, va_list _ap); #ifndef __STRICT_ANSI__ *** /develop/djgpp/src/docs/kb/wc204.txi Fri Oct 6 23:53:22 2000 --- /develop/djgpp.dev/src/docs/kb/wc204.txi Sun Nov 12 21:34:42 2000 *************** *** 131,133 **** --- 131,136 ---- @code{end}. The profiling code was also changed to not pollute the name space with @code{etext}. + @findex snprintf + @findex vsnprintf + New functions @code{snprintf} and @code{vsnprintf} added. *** /develop/djgpp/src/libc/ansi/stdio/flsbuf.c Thu Jun 3 18:27:34 1999 --- /develop/djgpp.dev/src/libc/ansi/stdio/flsbuf.c Fri Nov 17 23:36:04 2000 *************** *** 27,32 **** --- 27,36 ---- if ((f->_flag&_IOWRT)==0) return EOF; + /* No-op for full string buffers */ + if (f->_flag & _IOSTRG) + return c; + /* if the buffer is not yet allocated, allocate it */ if ((base = f->_base) == NULL && (f->_flag & _IONBF) == 0) { *** /develop/djgpp/src/libc/ansi/stdio/makefile Sun Jun 28 18:44:16 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/makefile Sun Nov 12 21:57:50 2000 *************** *** 1,3 **** --- 1,4 ---- + # Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details TOP=../.. *************** *** 61,65 **** --- 62,68 ---- SRC += vfprintf.c SRC += vprintf.c SRC += vsprintf.c + SRC += snprintf.c + SRC += vsnprntf.c include $(TOP)/../makefile.inc *** /develop/djgpp/src/libc/ansi/stdio/snprintf.c Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/snprintf.c Sat Nov 18 23:49:10 2000 *************** *** 1 **** --- 1,16 ---- + /* Copyright (C) 2000 DJ Delorie see COPYING.DJ for details */ + #include + #include + int + snprintf(char *str, size_t n, const char *fmt, ...) + { + va_list ap; + int len; + + va_start(ap, fmt); + len = vsnprintf(str, n, fmt, ap); + va_end(ap); + + return len; + } *** /develop/djgpp/src/libc/ansi/stdio/snprintf.txh Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/snprintf.txh Sat Nov 18 23:13:18 2000 *************** *** 1 **** --- 1,32 ---- + @node snprintf, stdio + @subheading Syntax + @example + #include + + int snprintf (char *@var{buffer}, size_t @var{n}, const char *@var{format}, + @dots{}); + @end example + + @subheading Description + + This function works similarly to @code{sprintf()} (@pxref{sprintf}), but + the size @var{n} of the @var{buffer} is also taken into account. This + function will write @var{n} - 1 characters. The @var{n}th character is used + for the terminating nul. If @var{n} is zero, @var{buffer} is not touched. + + @subheading Return Value + + The number of characters that would have been written (excluding the trailing + nul) is returned; otherwise -1 is returned to flag encoding or buffer space + errors. + + The maximum accepted value of @var{n} is @code{INT_MAX}. @code{INT_MAX} is + defined in @code{}. -1 is returned and @code{errno} is set to + @code{EFBIG}, if @var{n} is greater than this limit. + + @subheading Portability + + @port-note ansi The buffer size limit is imposed by DJGPP. Other systems may not have this limitation. + + @portability ansi *** /develop/djgpp/src/libc/ansi/stdio/sprintf.txh Sun Sep 27 16:20:50 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/sprintf.txh Sun Nov 12 21:37:08 2000 *************** *** 12,17 **** --- 12,20 ---- Sends formatted output from the arguments (@dots{}) to the @var{buffer}. @xref{printf}. + To avoid buffer overruns, it is safer to use @code{snprintf()} + (@pxref{snprintf}). + @subheading Return Value The number of characters written. *** /develop/djgpp/src/libc/ansi/stdio/vsnprntf.c Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsnprntf.c Sat Nov 18 23:45:00 2000 *************** *** 1 **** --- 1,43 ---- + /* Copyright (C) 2000 DJ Delorie, see COPYING.DJ for details */ + #include + #include + #include + #include + #include + int + vsnprintf(char *str, size_t n, const char *fmt, va_list ap) + { + FILE _strbuf; + int len; + + /* _cnt is an int in the FILE structure. To prevent wrap-around, we limit + * n to between 0 and INT_MAX inclusively. */ + if (n > INT_MAX) + { + errno = EFBIG; + return -1; + } + + _strbuf._flag = _IOWRT | _IOSTRG | _IONTERM; + + /* If n == 0, just querying how much space is needed. */ + if (n > 0) + { + _strbuf._cnt = n - 1; + _strbuf._ptr = str; + } + else + { + _strbuf._cnt = 0; + _strbuf._ptr = NULL; + } + + len = _doprnt(fmt, ap, &_strbuf); + + /* Ensure nul termination */ + if (n > 0) + *_strbuf._ptr = 0; + + return len; + } *** /develop/djgpp/src/libc/ansi/stdio/vsnprntf.txh Sun Nov 12 21:57:18 2000 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsnprntf.txh Sat Nov 18 23:13:40 2000 *************** *** 1 **** --- 1,33 ---- + @node vsnprintf, stdio + @subheading Syntax + @example + #include + #include + + int vsnprintf (char *@var{buffer}, size_t @var{n}, const char *@var{format}, + va_list @var{ap}); + @end example + + @subheading Description + + This function works similarly to @code{vsprintf()} (@pxref{vsprintf}), but + the size @var{n} of the @var{buffer} is also taken into account. This + function will write @var{n} - 1 characters. The @var{n}th character is used + for the terminating nul. If @var{n} is zero, @var{buffer} is not touched. + + @subheading Return Value + + The number of characters that would have been written (excluding the trailing + nul) is returned; otherwise -1 is returned to flag encoding or buffer space + errors. + + The maximum accepted value of @var{n} is @code{INT_MAX}. @code{INT_MAX} is + defined in @code{}. -1 is returned and @code{errno} is set to + @code{EFBIG}, if @var{n} is greater than this limit. + + @subheading Portability + + @port-note ansi The buffer size limit is imposed by DJGPP. Other systems may not have this limitation. + + @portability ansi *** /develop/djgpp/src/libc/ansi/stdio/vsprintf.txh Sun Sep 27 16:20:50 1998 --- /develop/djgpp.dev/src/libc/ansi/stdio/vsprintf.txh Sun Nov 12 21:37:00 2000 *************** *** 13,18 **** --- 13,21 ---- Sends formatted output from the @var{arguments} to the @var{buffer}. @xref{printf}. @xref{vfprintf}. + To avoid buffer overruns, it is safer to use @code{vsnprintf()} + (@pxref{vsnprintf}). + @subheading Return Value The number of characters written. *** /develop/djgpp/tests/libc/ansi/stdio/t-snprtf.c Sun Nov 19 00:04:32 2000 --- /develop/djgpp.dev/tests/libc/ansi/stdio/t-snprtf.c Sun Nov 19 00:03:24 2000 *************** *** 1 **** --- 1,112 ---- + /* + * t-snprtf.c - Test for snprintf() + */ + #include + #include + + int + main (void) + { + char holder[24]; + int i, j; + + #define BIG "Hello this is a too big string for the buffer" + i = snprintf (holder, sizeof (holder), "%s\n", BIG); + printf ("%s\n", BIG); + printf ("%s\n", holder); + /* + * We are expecting : + * i == strlen (BIG) + 1 + * meaning the number that would have been written if the buffer was + * large enough (see C9X). + */ + if (i != strlen (BIG) + 1/* NL */) + { + fprintf (stderr, "FAILED snprintf\n"); + fprintf (stderr, + "sizeof (%ld), snprintf(%d), strlen(%ld)\n", + sizeof (holder), i, strlen (BIG)) ; + exit (1); + } + + /* + * We may have broken sscanf since it is also a string stream + * Lets do a basic test. + */ + { + static char *line = "25 December 2000\n"; + int day, year; + char month[24]; + + /* we are expecting to read 3 variables */ + if ((i = sscanf (line, "%d %s %d\n", &day, month, &year)) == 3) + { + i = snprintf (holder, sizeof(holder), "%d %s %d\n", day, month, year); + printf (line); + j = printf (holder); + if (i != j) + { + fprintf (stderr, "FAILED snprintf\n"); + fprintf (stderr, "snprintf (%d) != printf (%d)\n", i, j); + exit (1); + } + } + else + { + printf ("sscanf (%d)\n", i); + printf ("FAILED sscanf\n"); + exit (1); + } + } + + /* Test length estimation - once with buffer, once without. */ + *holder = '\0'; + i = snprintf(holder, 0, "%s", BIG); + if ( (i != strlen(BIG)) || (*holder != '\0') /* buffer touched */) + { + fprintf (stderr, "FAILED length estimation (with buffer)\n"); + exit (1); + } + + i = snprintf(NULL, 0, "%s", BIG); + if (i != strlen(BIG)) + { + fprintf (stderr, "FAILED length estimation (no buffer)\n"); + exit (1); + } + + /* Try writing to a 1 byte buffer */ + snprintf(holder, sizeof(holder), "%s", BIG); + i = snprintf(holder, 1, "%s", BIG); + + if ((i < 0) || (*holder != '\0')) + { + fprintf (stderr, "FAILED termination only\n"); + exit (1); + } + + /* Test maximum buffer size */ + i = snprintf(holder, ((size_t) INT_MAX) + 1, "%s", BIG); + + if (i >= 0) + { + fprintf (stderr, "FAILED too large buffer\n"); + exit (1); + } + + /* Test handling of encoding errors */ + /* HOW? */ + + /*i = snprintf(holder, sizeof(holder), "%!", BIG);*/ + + /*if (i >= 0) + { + fprintf (stderr, "FAILED generating encoding error\n"); + exit (1); + }*/ + + /* signal success */ + printf ("SUCCESS\n"); + return 0; + }