]> git.ipfire.org Git - thirdparty/glibc.git/blob - debug/vasprintf_chk.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / debug / vasprintf_chk.c
1 /* Copyright (C) 1995,1997,1999-2002,2004,2006,2008,2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>.
18
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
27
28 #include <malloc.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdio_ext.h>
32 #include "../libio/libioP.h"
33 #include "../libio/strfile.h"
34
35 int
36 __vasprintf_chk (char **result_ptr, int flags, const char *format,
37 va_list args)
38 {
39 /* Initial size of the buffer to be used. Will be doubled each time an
40 overflow occurs. */
41 const _IO_size_t init_string_size = 100;
42 char *string;
43 _IO_strfile sf;
44 int ret;
45 _IO_size_t needed;
46 _IO_size_t allocated;
47 /* No need to clear the memory here (unlike for open_memstream) since
48 we know we will never seek on the stream. */
49 string = (char *) malloc (init_string_size);
50 if (string == NULL)
51 return -1;
52 #ifdef _IO_MTSAFE_IO
53 sf._sbf._f._lock = NULL;
54 #endif
55 _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
56 _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
57 _IO_str_init_static_internal (&sf, string, init_string_size, string);
58 sf._sbf._f._flags &= ~_IO_USER_BUF;
59 sf._s._allocate_buffer = (_IO_alloc_type) malloc;
60 sf._s._free_buffer = (_IO_free_type) free;
61
62 /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
63 can only come from read-only format strings. */
64 if (flags > 0)
65 sf._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
66
67 ret = INTUSE(_IO_vfprintf) (&sf._sbf._f, format, args);
68 if (ret < 0)
69 {
70 free (sf._sbf._f._IO_buf_base);
71 return ret;
72 }
73 /* Only use realloc if the size we need is of the same (binary)
74 order of magnitude then the memory we allocated. */
75 needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
76 allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
77 if ((allocated >> 1) <= needed)
78 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
79 else
80 {
81 *result_ptr = (char *) malloc (needed);
82 if (*result_ptr != NULL)
83 {
84 memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
85 free (sf._sbf._f._IO_buf_base);
86 }
87 else
88 /* We have no choice, use the buffer we already have. */
89 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
90 }
91 if (*result_ptr == NULL)
92 *result_ptr = sf._sbf._f._IO_buf_base;
93 (*result_ptr)[needed - 1] = '\0';
94 return ret;
95 }
96 libc_hidden_def (__vasprintf_chk)