Blame debug/vasprintf_chk.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
   As a special exception, if you link the code in this file with
Packit 6c4009
   files compiled with a GNU compiler to produce an executable,
Packit 6c4009
   that does not cause the resulting executable to be covered by
Packit 6c4009
   the GNU Lesser General Public License.  This exception does not
Packit 6c4009
   however invalidate any other reasons why the executable file
Packit 6c4009
   might be covered by the GNU Lesser General Public License.
Packit 6c4009
   This exception applies to code released by its copyright holders
Packit 6c4009
   in files containing the exception.  */
Packit 6c4009
Packit 6c4009
#include <malloc.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include "../libio/libioP.h"
Packit 6c4009
#include "../libio/strfile.h"
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__vasprintf_chk (char **result_ptr, int flags, const char *format,
Packit 6c4009
		 va_list args)
Packit 6c4009
{
Packit 6c4009
  /* Initial size of the buffer to be used.  Will be doubled each time an
Packit 6c4009
     overflow occurs.  */
Packit 6c4009
  const size_t init_string_size = 100;
Packit 6c4009
  char *string;
Packit 6c4009
  _IO_strfile sf;
Packit 6c4009
  int ret;
Packit 6c4009
  size_t needed;
Packit 6c4009
  size_t allocated;
Packit 6c4009
  /* No need to clear the memory here (unlike for open_memstream) since
Packit 6c4009
     we know we will never seek on the stream.  */
Packit 6c4009
  string = (char *) malloc (init_string_size);
Packit 6c4009
  if (string == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  sf._sbf._f._lock = NULL;
Packit 6c4009
#endif
Packit 6c4009
  _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
Packit 6c4009
  _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
Packit 6c4009
  _IO_str_init_static_internal (&sf, string, init_string_size, string);
Packit 6c4009
  sf._sbf._f._flags &= ~_IO_USER_BUF;
Packit 6c4009
  sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
Packit 6c4009
  sf._s._free_buffer_unused = (_IO_free_type) free;
Packit 6c4009
Packit 6c4009
  /* For flags > 0 (i.e. __USE_FORTIFY_LEVEL > 1) request that %n
Packit 6c4009
     can only come from read-only format strings.  */
Packit 6c4009
  if (flags > 0)
Packit 6c4009
    sf._sbf._f._flags2 |= _IO_FLAGS2_FORTIFY;
Packit 6c4009
Packit 6c4009
  ret = _IO_vfprintf (&sf._sbf._f, format, args);
Packit 6c4009
  if (ret < 0)
Packit 6c4009
    {
Packit 6c4009
      free (sf._sbf._f._IO_buf_base);
Packit 6c4009
      return ret;
Packit 6c4009
    }
Packit 6c4009
  /* Only use realloc if the size we need is of the same (binary)
Packit 6c4009
     order of magnitude then the memory we allocated.  */
Packit 6c4009
  needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
Packit 6c4009
  allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
Packit 6c4009
  if ((allocated >> 1) <= needed)
Packit 6c4009
    *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      *result_ptr = (char *) malloc (needed);
Packit 6c4009
      if (*result_ptr != NULL)
Packit 6c4009
	{
Packit 6c4009
	  memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed - 1);
Packit 6c4009
	  free (sf._sbf._f._IO_buf_base);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* We have no choice, use the buffer we already have.  */
Packit 6c4009
	*result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
Packit 6c4009
    }
Packit 6c4009
  if (*result_ptr == NULL)
Packit 6c4009
    *result_ptr = sf._sbf._f._IO_buf_base;
Packit 6c4009
  (*result_ptr)[needed - 1] = '\0';
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__vasprintf_chk)