Blame malloc/scratch_buffer_grow_preserve.c

Packit 6c4009
/* Variable-sized buffer with on-stack default allocation.
Packit 6c4009
   Copyright (C) 2015-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
#ifndef _LIBC
Packit 6c4009
# include <libc-config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <scratch_buffer.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
bool
Packit 6c4009
__libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
Packit 6c4009
{
Packit 6c4009
  size_t new_length = 2 * buffer->length;
Packit 6c4009
  void *new_ptr;
Packit 6c4009
Packit 6c4009
  if (buffer->data == buffer->__space.__c)
Packit 6c4009
    {
Packit 6c4009
      /* Move buffer to the heap.  No overflow is possible because
Packit 6c4009
	 buffer->length describes a small buffer on the stack.  */
Packit 6c4009
      new_ptr = malloc (new_length);
Packit 6c4009
      if (new_ptr == NULL)
Packit 6c4009
	return false;
Packit 6c4009
      memcpy (new_ptr, buffer->__space.__c, buffer->length);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Buffer was already on the heap.  Check for overflow.  */
Packit 6c4009
      if (__glibc_likely (new_length >= buffer->length))
Packit 6c4009
	new_ptr = realloc (buffer->data, new_length);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ENOMEM);
Packit 6c4009
	  new_ptr = NULL;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__glibc_unlikely (new_ptr == NULL))
Packit 6c4009
	{
Packit 6c4009
	  /* Deallocate, but buffer must remain valid to free.  */
Packit 6c4009
	  free (buffer->data);
Packit 6c4009
	  scratch_buffer_init (buffer);
Packit 6c4009
	  return false;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Install new heap-based buffer.  */
Packit 6c4009
  buffer->data = new_ptr;
Packit 6c4009
  buffer->length = new_length;
Packit 6c4009
  return true;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__libc_scratch_buffer_grow_preserve)