1 | /* Copyright (C) 1991-2017 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 <wchar.h> |
20 | #include "../libio/libioP.h" |
21 | #include "../libio/strfile.h" |
22 | |
23 | |
24 | /* Write formatted output into S, according to the format |
25 | string FORMAT, writing no more than MAXLEN characters. */ |
26 | /* VARARGS5 */ |
27 | int |
28 | __vswprintf_chk (wchar_t *s, size_t maxlen, int flags, size_t slen, |
29 | const wchar_t *format, va_list args) |
30 | { |
31 | /* XXX Maybe for less strict version do not fail immediately. |
32 | Though, maxlen is supposed to be the size of buffer pointed |
33 | to by s, so a conforming program can't pass such maxlen |
34 | to *snprintf. */ |
35 | if (__glibc_unlikely (slen < maxlen)) |
36 | __chk_fail (); |
37 | |
38 | _IO_wstrnfile sf; |
39 | struct _IO_wide_data wd; |
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 (__glibc_unlikely (maxlen == 0)) |
48 | /* Since we have to write at least the terminating L'\0' a buffer |
49 | length of zero always makes the function fail. */ |
50 | return -1; |
51 | |
52 | _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps); |
53 | _IO_fwide (&sf.f._sbf._f, 1); |
54 | s[0] = L'\0'; |
55 | |
56 | /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n |
57 | can only come from read-only format strings. */ |
58 | if (flags > 0) |
59 | sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY; |
60 | |
61 | _IO_wstr_init_static (&sf.f._sbf._f, s, maxlen - 1, s); |
62 | ret = _IO_vfwprintf ((_IO_FILE *) &sf.f._sbf, format, args); |
63 | |
64 | if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf) |
65 | /* ISO C99 requires swprintf/vswprintf to return an error if the |
66 | output does not fit int he provided buffer. */ |
67 | return -1; |
68 | |
69 | /* Terminate the string. */ |
70 | *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0'; |
71 | |
72 | return ret; |
73 | } |
74 | libc_hidden_def (__vswprintf_chk) |
75 | |