update to 2.17
[platform/upstream/glibc.git] / debug / vsnprintf_chk.c
1 /* Copyright (C) 1991-2015 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include "../libio/libioP.h"
21 #include "../libio/strfile.h"
22
23 extern const struct _IO_jump_t _IO_strn_jumps attribute_hidden;
24
25 /* Write formatted output into S, according to the format
26    string FORMAT, writing no more than MAXLEN characters.  */
27 /* VARARGS5 */
28 int
29 ___vsnprintf_chk (char *s, size_t maxlen, int flags, size_t slen,
30                   const char *format, va_list args)
31 {
32   /* XXX Maybe for less strict version do not fail immediately.
33      Though, maxlen is supposed to be the size of buffer pointed
34      to by s, so a conforming program can't pass such maxlen
35      to *snprintf.  */
36   if (__glibc_unlikely (slen < maxlen))
37     __chk_fail ();
38
39   _IO_strnfile sf;
40   int ret;
41 #ifdef _IO_MTSAFE_IO
42   sf.f._sbf._f._lock = NULL;
43 #endif
44
45   /* We need to handle the special case where MAXLEN is 0.  Use the
46      overflow buffer right from the start.  */
47   if (maxlen == 0)
48     {
49       s = sf.overflow_buf;
50       maxlen = sizeof (sf.overflow_buf);
51     }
52
53   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
54   _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
55   s[0] = '\0';
56
57   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
58      can only come from read-only format strings.  */
59   if (flags > 0)
60     sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
61
62   _IO_str_init_static_internal (&sf.f, s, maxlen - 1, s);
63   ret = _IO_vfprintf (&sf.f._sbf._f, format, args);
64
65   if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
66     *sf.f._sbf._f._IO_write_ptr = '\0';
67   return ret;
68 }
69 ldbl_hidden_def (___vsnprintf_chk, __vsnprintf_chk)
70 ldbl_strong_alias (___vsnprintf_chk, __vsnprintf_chk)