malloc: Prevent arena free_list from turning cyclic [BZ #19048]
[glibc.git] / debug / obprintf_chk.c
blobc49d1e9cd8d5e0fef5e235c643bab0c084fb4989
1 /* Print output of stream to given obstack.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
21 #include <stdlib.h>
22 #include <libioP.h>
23 #include "../libio/strfile.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <obstack.h>
28 #include <stdarg.h>
29 #include <stdio_ext.h>
32 struct _IO_obstack_file
34 struct _IO_FILE_plus file;
35 struct obstack *obstack;
38 extern const struct _IO_jump_t _IO_obstack_jumps attribute_hidden;
40 int
41 __obstack_vprintf_chk (struct obstack *obstack, int flags, const char *format,
42 va_list args)
44 struct obstack_FILE
46 struct _IO_obstack_file ofile;
47 } new_f;
48 int result;
49 int size;
50 int room;
52 #ifdef _IO_MTSAFE_IO
53 new_f.ofile.file.file._lock = NULL;
54 #endif
56 _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
57 _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
58 room = obstack_room (obstack);
59 size = obstack_object_size (obstack) + room;
60 if (size == 0)
62 /* We have to handle the allocation a bit different since the
63 `_IO_str_init_static' function would handle a size of zero
64 different from what we expect. */
66 /* Get more memory. */
67 obstack_make_room (obstack, 64);
69 /* Recompute how much room we have. */
70 room = obstack_room (obstack);
71 size = room;
73 assert (size != 0);
76 _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
77 obstack_base (obstack),
78 size, obstack_next_free (obstack));
79 /* Now allocate the rest of the current chunk. */
80 assert (size == (new_f.ofile.file.file._IO_write_end
81 - new_f.ofile.file.file._IO_write_base));
82 assert (new_f.ofile.file.file._IO_write_ptr
83 == (new_f.ofile.file.file._IO_write_base
84 + obstack_object_size (obstack)));
85 obstack_blank_fast (obstack, room);
87 new_f.ofile.obstack = obstack;
89 /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
90 can only come from read-only format strings. */
91 if (flags > 0)
92 new_f.ofile.file.file._flags2 |= _IO_FLAGS2_FORTIFY;
94 result = _IO_vfprintf (&new_f.ofile.file.file, format, args);
96 /* Shrink the buffer to the space we really currently need. */
97 obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
98 - new_f.ofile.file.file._IO_write_end));
100 return result;
102 libc_hidden_def (__obstack_vprintf_chk)
106 __obstack_printf_chk (struct obstack *obstack, int flags, const char *format,
107 ...)
109 int result;
110 va_list ap;
111 va_start (ap, format);
112 result = __obstack_vprintf_chk (obstack, flags, format, ap);
113 va_end (ap);
114 return result;