Back to index

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