83383286da18f60982c8bef5c09fded9ebb2e359
[platform/upstream/glibc.git] / debug / vsprintf_chk.c
1 /* Copyright (C) 1994, 1997, 1999-2003, 2004 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 <stdio.h>
21 #include "../libio/libioP.h"
22 #include "../libio/strfile.h"
23
24
25 static int _IO_str_chk_overflow (_IO_FILE *fp, int c) __THROW;
26
27 static int
28 _IO_str_chk_overflow (fp, c)
29      _IO_FILE *fp;
30      int c;
31 {
32   /* When we come to here this means the user supplied buffer is
33      filled.  */
34   __chk_fail ();
35 }
36
37
38 static const struct _IO_jump_t _IO_str_chk_jumps =
39 {
40   JUMP_INIT_DUMMY,
41   JUMP_INIT(finish, _IO_str_finish),
42   JUMP_INIT(overflow, _IO_str_chk_overflow),
43   JUMP_INIT(underflow, INTUSE(_IO_str_underflow)),
44   JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
45   JUMP_INIT(pbackfail, INTUSE(_IO_str_pbackfail)),
46   JUMP_INIT(xsputn, INTUSE(_IO_default_xsputn)),
47   JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
48   JUMP_INIT(seekoff, INTUSE(_IO_str_seekoff)),
49   JUMP_INIT(seekpos, _IO_default_seekpos),
50   JUMP_INIT(setbuf, _IO_default_setbuf),
51   JUMP_INIT(sync, _IO_default_sync),
52   JUMP_INIT(doallocate, INTUSE(_IO_default_doallocate)),
53   JUMP_INIT(read, _IO_default_read),
54   JUMP_INIT(write, _IO_default_write),
55   JUMP_INIT(seek, _IO_default_seek),
56   JUMP_INIT(close, _IO_default_close),
57   JUMP_INIT(stat, _IO_default_stat),
58   JUMP_INIT(showmanyc, _IO_default_showmanyc),
59   JUMP_INIT(imbue, _IO_default_imbue)
60 };
61
62
63 int
64 __vsprintf_chk (char *s, int flags, size_t slen, const char *format,
65                 va_list args)
66 {
67   _IO_strfile f;
68   int ret;
69 #ifdef _IO_MTSAFE_IO
70   f._sbf._f._lock = NULL;
71 #endif
72
73   if (slen == 0)
74     __chk_fail ();
75
76   _IO_no_init (&f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
77   _IO_JUMPS ((struct _IO_FILE_plus *) &f._sbf) = &_IO_str_chk_jumps;
78   s[0] = '\0';
79   _IO_str_init_static_internal (&f, s, slen - 1, s);
80
81   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
82      can only come from read-only format strings.  */
83   if (flags > 0)
84     f._sbf._f._flags2 |= _IO_FLAGS2_CHECK_PERCENT_N;
85
86   ret = INTUSE(_IO_vfprintf) ((_IO_FILE *) &f._sbf, format, args);
87
88   *f._sbf._f._IO_write_ptr = '\0';
89   return ret;
90 }
91 libc_hidden_def (__vsprintf_chk)