Back to index

glibc  2.9
vswprintf_chk.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1995,1997,1998,2004,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdarg.h>
00020 #include <wchar.h>
00021 #include "../libio/libioP.h"
00022 #include "../libio/strfile.h"
00023 
00024 
00025 /* Write formatted output into S, according to the format
00026    string FORMAT, writing no more than MAXLEN characters.  */
00027 /* VARARGS5 */
00028 int
00029 __vswprintf_chk (wchar_t *s, size_t maxlen, int flags, size_t slen,
00030                const wchar_t *format, va_list args)
00031 {
00032   /* XXX Maybe for less strict version do not fail immediately.
00033      Though, maxlen is supposed to be the size of buffer pointed
00034      to by s, so a conforming program can't pass such maxlen
00035      to *snprintf.  */
00036   if (__builtin_expect (slen < maxlen, 0))
00037     __chk_fail ();
00038 
00039   _IO_wstrnfile sf;
00040   struct _IO_wide_data wd;
00041   int ret;
00042 #ifdef _IO_MTSAFE_IO
00043   sf.f._sbf._f._lock = NULL;
00044 #endif
00045 
00046   /* We need to handle the special case where MAXLEN is 0.  Use the
00047      overflow buffer right from the start.  */
00048   if (__builtin_expect (maxlen == 0, 0))
00049     /* Since we have to write at least the terminating L'\0' a buffer
00050        length of zero always makes the function fail.  */
00051     return -1;
00052 
00053   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
00054   _IO_fwide (&sf.f._sbf._f, 1);
00055   s[0] = L'\0';
00056 
00057   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
00058      can only come from read-only format strings.  */
00059   if (flags > 0)
00060     sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
00061 
00062   _IO_wstr_init_static (&sf.f._sbf._f, s, maxlen - 1, s);
00063   ret = _IO_vfwprintf ((_IO_FILE *) &sf.f._sbf, format, args);
00064 
00065   if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
00066     /* ISO C99 requires swprintf/vswprintf to return an error if the
00067        output does not fit int he provided buffer.  */
00068     return -1;
00069 
00070   /* Terminate the string.  */
00071   *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
00072 
00073   return ret;
00074 }
00075 libc_hidden_def (__vswprintf_chk)