Blame debug/vswprintf_chk.c

Packit Service 82fcde
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <stdarg.h>
Packit Service 82fcde
#include <wchar.h>
Packit Service 82fcde
#include "../libio/libioP.h"
Packit Service 82fcde
#include "../libio/strfile.h"
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* Write formatted output into S, according to the format
Packit Service 82fcde
   string FORMAT, writing no more than MAXLEN characters.  */
Packit Service 82fcde
/* VARARGS5 */
Packit Service 82fcde
int
Packit Service 82fcde
__vswprintf_chk (wchar_t *s, size_t maxlen, int flags, size_t slen,
Packit Service 82fcde
		 const wchar_t *format, va_list args)
Packit Service 82fcde
{
Packit Service 82fcde
  /* XXX Maybe for less strict version do not fail immediately.
Packit Service 82fcde
     Though, maxlen is supposed to be the size of buffer pointed
Packit Service 82fcde
     to by s, so a conforming program can't pass such maxlen
Packit Service 82fcde
     to *snprintf.  */
Packit Service 82fcde
  if (__glibc_unlikely (slen < maxlen))
Packit Service 82fcde
    __chk_fail ();
Packit Service 82fcde
Packit Service 82fcde
  _IO_wstrnfile sf;
Packit Service 82fcde
  struct _IO_wide_data wd;
Packit Service 82fcde
  int ret;
Packit Service 82fcde
#ifdef _IO_MTSAFE_IO
Packit Service 82fcde
  sf.f._sbf._f._lock = NULL;
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
  /* We need to handle the special case where MAXLEN is 0.  Use the
Packit Service 82fcde
     overflow buffer right from the start.  */
Packit Service 82fcde
  if (__glibc_unlikely (maxlen == 0))
Packit Service 82fcde
    /* Since we have to write at least the terminating L'\0' a buffer
Packit Service 82fcde
       length of zero always makes the function fail.  */
Packit Service 82fcde
    return -1;
Packit Service 82fcde
Packit Service 82fcde
  _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
Packit Service 82fcde
  _IO_fwide (&sf.f._sbf._f, 1);
Packit Service 82fcde
  s[0] = L'\0';
Packit Service 82fcde
Packit Service 82fcde
  /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
Packit Service 82fcde
     can only come from read-only format strings.  */
Packit Service 82fcde
  if (flags > 0)
Packit Service 82fcde
    sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
Packit Service 82fcde
Packit Service 82fcde
  _IO_wstr_init_static (&sf.f._sbf._f, s, maxlen - 1, s);
Packit Service 82fcde
  ret = _IO_vfwprintf ((FILE *) &sf.f._sbf, format, args);
Packit Service 82fcde
Packit Service 82fcde
  if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
Packit Service 82fcde
    /* ISO C99 requires swprintf/vswprintf to return an error if the
Packit Service 82fcde
       output does not fit int he provided buffer.  */
Packit Service 82fcde
    return -1;
Packit Service 82fcde
Packit Service 82fcde
  /* Terminate the string.  */
Packit Service 82fcde
  *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
Packit Service 82fcde
Packit Service 82fcde
  return ret;
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_def (__vswprintf_chk)