Update copyright year
[platform/upstream/glibc.git] / debug / vswprintf_chk.c
1 /* Copyright (C) 1991,1995,1997,1998,2004,2005 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <stdarg.h>
20 #include <wchar.h>
21 #include "../libio/libioP.h"
22 #include "../libio/strfile.h"
23
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 __vswprintf_chk (wchar_t *s, size_t maxlen, int flags, size_t slen,
30                  const wchar_t *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 (__builtin_expect (slen < maxlen, 0))
37     __chk_fail ();
38
39   _IO_wstrnfile sf;
40   struct _IO_wide_data wd;
41   int ret;
42 #ifdef _IO_MTSAFE_IO
43   sf.f._sbf._f._lock = NULL;
44 #endif
45
46   /* We need to handle the special case where MAXLEN is 0.  Use the
47      overflow buffer right from the start.  */
48   if (__builtin_expect (maxlen == 0, 0))
49     /* Since we have to write at least the terminating L'\0' a buffer
50        length of zero always makes the function fail.  */
51     return -1;
52
53   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
54   _IO_fwide (&sf.f._sbf._f, 1);
55   s[0] = L'\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_wstr_init_static (&sf.f._sbf._f, s, maxlen - 1, s);
63   ret = _IO_vfwprintf ((_IO_FILE *) &sf.f._sbf, format, args);
64
65   if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
66     /* ISO C99 requires swprintf/vswprintf to return an error if the
67        output does not fit int he provided buffer.  */
68     return -1;
69
70   /* Terminate the string.  */
71   *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
72
73   return ret;
74 }
75 libc_hidden_def (__vswprintf_chk)