(svc_run): Fail instead of segfaulting if malloc crashed. Don't allocate
[glibc/history.git] / debug / vsnprintf_chk.c
blobc46937ff6fbf72ce025bedca8c5579b215a21b5d
1 /* Copyright (C) 1991, 1995, 1997, 1998, 2004, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include "../libio/libioP.h"
23 #include "../libio/strfile.h"
25 extern const struct _IO_jump_t _IO_strn_jumps attribute_hidden;
27 /* Write formatted output into S, according to the format
28 string FORMAT, writing no more than MAXLEN characters. */
29 /* VARARGS5 */
30 int
31 ___vsnprintf_chk (char *s, size_t maxlen, int flags, size_t slen,
32 const char *format, va_list args)
34 /* XXX Maybe for less strict version do not fail immediately.
35 Though, maxlen is supposed to be the size of buffer pointed
36 to by s, so a conforming program can't pass such maxlen
37 to *snprintf. */
38 if (__builtin_expect (slen < maxlen, 0))
39 __chk_fail ();
41 _IO_strnfile sf;
42 int ret;
43 #ifdef _IO_MTSAFE_IO
44 sf.f._sbf._f._lock = NULL;
45 #endif
47 /* We need to handle the special case where MAXLEN is 0. Use the
48 overflow buffer right from the start. */
49 if (maxlen == 0)
51 s = sf.overflow_buf;
52 maxlen = sizeof (sf.overflow_buf);
55 _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
56 _IO_JUMPS ((struct _IO_FILE_plus *) &sf.f._sbf) = &_IO_strn_jumps;
57 s[0] = '\0';
59 /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
60 can only come from read-only format strings. */
61 if (flags > 0)
62 sf.f._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
64 _IO_str_init_static_internal (&sf.f, s, maxlen - 1, s);
65 ret = INTUSE(_IO_vfprintf) ((_IO_FILE *) &sf.f._sbf, format, args);
67 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
68 *sf.f._sbf._f._IO_write_ptr = '\0';
69 return ret;
71 ldbl_hidden_def (___vsnprintf_chk, __vsnprintf_chk)
72 ldbl_strong_alias (___vsnprintf_chk, __vsnprintf_chk)