[BZ #1877]
[glibc.git] / debug / vsnprintf_chk.c
bloba0fd4ebfdc6f04fd03bf57180331fe60da980636
1 /* Copyright (C) 1991, 1995, 1997, 1998, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include "../libio/libioP.h"
22 #include "../libio/strfile.h"
24 extern const struct _IO_jump_t _IO_strn_jumps attribute_hidden;
26 /* Write formatted output into S, according to the format
27 string FORMAT, writing no more than MAXLEN characters. */
28 /* VARARGS5 */
29 int
30 __vsnprintf_chk (char *s, size_t maxlen, int flags, size_t slen,
31 const char *format, va_list args)
33 /* XXX Maybe for less strict version do not fail immediately.
34 Though, maxlen is supposed to be the size of buffer pointed
35 to by s, so a conforming program can't pass such maxlen
36 to *snprintf. */
37 if (__builtin_expect (slen < maxlen, 0))
38 __chk_fail ();
40 _IO_strnfile sf;
41 int ret;
42 #ifdef _IO_MTSAFE_IO
43 sf.f._sbf._f._lock = NULL;
44 #endif
46 /* We need to handle the special case where MAXLEN is 0. Use the
47 overflow buffer right from the start. */
48 if (maxlen == 0)
50 s = sf.overflow_buf;
51 maxlen = sizeof (sf.overflow_buf);
54 _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
55 _IO_JUMPS ((struct _IO_FILE_plus *) &sf.f._sbf) = &_IO_strn_jumps;
56 s[0] = '\0';
58 /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
59 can only come from read-only format strings. */
60 if (flags > 0)
61 sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
63 _IO_str_init_static_internal (&sf.f, s, maxlen - 1, s);
64 ret = INTUSE(_IO_vfprintf) ((_IO_FILE *) &sf.f._sbf, format, args);
66 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
67 *sf.f._sbf._f._IO_write_ptr = '\0';
68 return ret;
70 libc_hidden_def (__vsnprintf_chk)