Back to index

glibc  2.9
vasprintf_chk.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995,1997,1999-2002,2004,2006,2008
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    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 #include <malloc.h>
00030 #include <string.h>
00031 #include <stdio.h>
00032 #include <stdio_ext.h>
00033 #include "../libio/libioP.h"
00034 #include "../libio/strfile.h"
00035 
00036 int
00037 __vasprintf_chk (char **result_ptr, int flags, const char *format,
00038                va_list args)
00039 {
00040   /* Initial size of the buffer to be used.  Will be doubled each time an
00041      overflow occurs.  */
00042   const _IO_size_t init_string_size = 100;
00043   char *string;
00044   _IO_strfile sf;
00045   int ret;
00046   _IO_size_t needed;
00047   _IO_size_t allocated;
00048   /* No need to clear the memory here (unlike for open_memstream) since
00049      we know we will never seek on the stream.  */
00050   string = (char *) malloc (init_string_size);
00051   if (string == NULL)
00052     return -1;
00053 #ifdef _IO_MTSAFE_IO
00054   sf._sbf._f._lock = NULL;
00055 #endif
00056   _IO_no_init ((_IO_FILE *) &sf._sbf, _IO_USER_LOCK, -1, NULL, NULL);
00057   _IO_JUMPS ((struct _IO_FILE_plus *) &sf._sbf) = &_IO_str_jumps;
00058   _IO_str_init_static_internal (&sf, string, init_string_size, string);
00059   sf._sbf._f._flags &= ~_IO_USER_BUF;
00060   sf._s._allocate_buffer = (_IO_alloc_type) malloc;
00061   sf._s._free_buffer = (_IO_free_type) free;
00062 
00063   /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
00064      can only come from read-only format strings.  */
00065   if (flags > 0)
00066     sf._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
00067 
00068   ret = INTUSE(_IO_vfprintf) (&sf._sbf._f, format, args);
00069   if (ret < 0)
00070     {
00071       free (sf._sbf._f._IO_buf_base);
00072       return ret;
00073     }
00074   /* Only use realloc if the size we need is of the same (binary)
00075      order of magnitude then the memory we allocated.  */
00076   needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
00077   allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
00078   if ((allocated >> 1) <= needed)
00079     *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
00080   else
00081     {
00082       *result_ptr = (char *) malloc (needed);
00083       if (*result_ptr != NULL)
00084        {
00085          memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
00086          free (sf._sbf._f._IO_buf_base);
00087        }
00088       else
00089        /* We have no choice, use the buffer we already have.  */
00090        *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
00091     }
00092   if (*result_ptr == NULL)
00093     *result_ptr = sf._sbf._f._IO_buf_base;
00094   (*result_ptr)[needed - 1] = '\0';
00095   return ret;
00096 }
00097 libc_hidden_def (__vasprintf_chk)