Replace FSF snail mail address with URLs.
[glibc.git] / debug / vsnprintf_chk.c
blob258a05025266628caf75867aad5a6680dd9dd1e3
1 /* Copyright (C) 1991, 1995, 1997, 1998, 2004, 2006, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
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 (&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) (&sf.f._sbf._f, 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 ldbl_hidden_def (___vsnprintf_chk, __vsnprintf_chk)
71 ldbl_strong_alias (___vsnprintf_chk, __vsnprintf_chk)