Blame SOURCES/glibc-rh677316-scratch_buffer.patch

fa3bfd
Backport of the following upstream commits:
fa3bfd
fa3bfd
commit c04af6068b2c2c65c92b3360cbc4b51ad9843f21
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Thu Apr 7 13:46:28 2016 +0200
fa3bfd
fa3bfd
    scratch_buffer_set_array_size: Include <limits.h>
fa3bfd
fa3bfd
    It is needed for CHAR_BIT.
fa3bfd
fa3bfd
commit dbb7600658d8ea633083ee99572622b04ef23a3f
fa3bfd
Author: Joseph Myers <joseph@codesourcery.com>
fa3bfd
Date:   Thu Oct 29 12:46:22 2015 +0000
fa3bfd
fa3bfd
    Use max_align_t from <stddef.h>.
fa3bfd
fa3bfd
    Now that we build with -std=gnu11 and can rely on a compiler providing
fa3bfd
    max_align_t in <stddef.h>, we no longer need our own version
fa3bfd
    libc_max_align_t.  This patch removes it and replaces the single user
fa3bfd
    with a use of max_align_t.
fa3bfd
fa3bfd
    Tested for x86_64 and x86 (testsuite, and that installed stripped
fa3bfd
    shared libraries are unchanged by the patch for x86_64; for x86, I see
fa3bfd
    some code reordering of no significance).
fa3bfd
fa3bfd
            * include/libc-internal.h (libc_max_align_t): Remove typedef.
fa3bfd
            * include/scratch_buffer.h: Include <stddef.h> instead of
fa3bfd
            <libc-internal.h>.
fa3bfd
            (struct scratch_buffer): Use max_align_t instead of
fa3bfd
            libc_max_align_t.
fa3bfd
fa3bfd
(The above is a partial backport.  It depends on max_align_t addition to
fa3bfd
<include/sys/cdefs.h> because glibc 2.17 is not compiled in C11 mode, so
fa3bfd
GCC's <stddef.h> does not provide it.)
fa3bfd
fa3bfd
commit 2902af1631c0c74c2f0c0edd7a85a523370e5027
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Thu Apr 9 17:12:42 2015 +0200
fa3bfd
fa3bfd
    scratch_buffer: Suppress truncation warning on 32-bit
fa3bfd
fa3bfd
commit 72301304a5655662baf2bae88a7aceeabc4a753e
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Tue Apr 7 17:46:58 2015 +0200
fa3bfd
fa3bfd
    scratch_buffer_grow_preserve: Add missing #include <string.h>
fa3bfd
fa3bfd
commit cfcfd4614b8b01b2782ac4dcafb21d14d74d5184
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Tue Apr 7 11:03:43 2015 +0200
fa3bfd
fa3bfd
    Add struct scratch_buffer and its internal helper functions
fa3bfd
fa3bfd
    These will be used from NSS modules, so they have to be exported.
fa3bfd
fa3bfd
diff --git a/include/scratch_buffer.h b/include/scratch_buffer.h
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..dd17a4a7e13596aa
fa3bfd
--- /dev/null
fa3bfd
+++ b/include/scratch_buffer.h
fa3bfd
@@ -0,0 +1,136 @@
fa3bfd
+/* Variable-sized buffer with on-stack default allocation.
fa3bfd
+   Copyright (C) 2015-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#ifndef _SCRATCH_BUFFER_H
fa3bfd
+#define _SCRATCH_BUFFER_H
fa3bfd
+
fa3bfd
+/* Scratch buffers with a default stack allocation and fallback to
fa3bfd
+   heap allocation.  It is expected that this function is used in this
fa3bfd
+   way:
fa3bfd
+
fa3bfd
+     struct scratch_buffer tmpbuf;
fa3bfd
+     scratch_buffer_init (&tmpbuf);
fa3bfd
+
fa3bfd
+     while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
fa3bfd
+       if (!scratch_buffer_grow (&tmpbuf))
fa3bfd
+	 return -1;
fa3bfd
+
fa3bfd
+     scratch_buffer_free (&tmpbuf);
fa3bfd
+     return 0;
fa3bfd
+
fa3bfd
+   The allocation functions (scratch_buffer_grow,
fa3bfd
+   scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make
fa3bfd
+   sure that the heap allocation, if any, is freed, so that the code
fa3bfd
+   above does not have a memory leak.  The buffer still remains in a
fa3bfd
+   state that can be deallocated using scratch_buffer_free, so a loop
fa3bfd
+   like this is valid as well:
fa3bfd
+
fa3bfd
+     struct scratch_buffer tmpbuf;
fa3bfd
+     scratch_buffer_init (&tmpbuf);
fa3bfd
+
fa3bfd
+     while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
fa3bfd
+       if (!scratch_buffer_grow (&tmpbuf))
fa3bfd
+	 break;
fa3bfd
+
fa3bfd
+     scratch_buffer_free (&tmpbuf);
fa3bfd
+
fa3bfd
+   scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed
fa3bfd
+   to grow the buffer by at least 512 bytes.  This means that when
fa3bfd
+   using the scratch buffer as a backing store for a non-character
fa3bfd
+   array whose element size, in bytes, is 512 or smaller, the scratch
fa3bfd
+   buffer only has to grow once to make room for at least one more
fa3bfd
+   element.
fa3bfd
+*/
fa3bfd
+
fa3bfd
+#include <stdbool.h>
fa3bfd
+#include <stddef.h>
fa3bfd
+#include <stdlib.h>
fa3bfd
+
fa3bfd
+/* Scratch buffer.  Must be initialized with scratch_buffer_init
fa3bfd
+   before its use.  */
fa3bfd
+struct scratch_buffer {
fa3bfd
+  void *data;    /* Pointer to the beginning of the scratch area.  */
fa3bfd
+  size_t length; /* Allocated space at the data pointer, in bytes.  */
fa3bfd
+  char __space[1024]
fa3bfd
+    __attribute__ ((aligned (__alignof__ (max_align_t))));
fa3bfd
+};
fa3bfd
+
fa3bfd
+/* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space
fa3bfd
+   and BUFFER->length reflects the available space.  */
fa3bfd
+static inline void
fa3bfd
+scratch_buffer_init (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  buffer->data = buffer->__space;
fa3bfd
+  buffer->length = sizeof (buffer->__space);
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Deallocates *BUFFER (if it was heap-allocated).  */
fa3bfd
+static inline void
fa3bfd
+scratch_buffer_free (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  if (buffer->data != buffer->__space)
fa3bfd
+    free (buffer->data);
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Grow *BUFFER by some arbitrary amount.  The buffer contents is NOT
fa3bfd
+   preserved.  Return true on success, false on allocation failure (in
fa3bfd
+   which case the old buffer is freed).  On success, the new buffer is
fa3bfd
+   larger than the previous size.  On failure, *BUFFER is deallocated,
fa3bfd
+   but remains in a free-able state, and errno is set.  */
fa3bfd
+bool __libc_scratch_buffer_grow (struct scratch_buffer *buffer);
fa3bfd
+libc_hidden_proto (__libc_scratch_buffer_grow)
fa3bfd
+
fa3bfd
+/* Alias for __libc_scratch_buffer_grow.  */
fa3bfd
+static __always_inline bool
fa3bfd
+scratch_buffer_grow (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  return __glibc_likely (__libc_scratch_buffer_grow (buffer));
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Like __libc_scratch_buffer_grow, but preserve the old buffer
fa3bfd
+   contents on success, as a prefix of the new buffer.  */
fa3bfd
+bool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer);
fa3bfd
+libc_hidden_proto (__libc_scratch_buffer_grow_preserve)
fa3bfd
+
fa3bfd
+/* Alias for __libc_scratch_buffer_grow_preserve.  */
fa3bfd
+static __always_inline bool
fa3bfd
+scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  return __glibc_likely (__libc_scratch_buffer_grow_preserve (buffer));
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Grow *BUFFER so that it can store at least NELEM elements of SIZE
fa3bfd
+   bytes.  The buffer contents are NOT preserved.  Both NELEM and SIZE
fa3bfd
+   can be zero.  Return true on success, false on allocation failure
fa3bfd
+   (in which case the old buffer is freed, but *BUFFER remains in a
fa3bfd
+   free-able state, and errno is set).  It is unspecified whether this
fa3bfd
+   function can reduce the array size.  */
fa3bfd
+bool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
fa3bfd
+					   size_t nelem, size_t size);
fa3bfd
+libc_hidden_proto (__libc_scratch_buffer_set_array_size)
fa3bfd
+
fa3bfd
+/* Alias for __libc_scratch_set_array_size.  */
fa3bfd
+static __always_inline bool
fa3bfd
+scratch_buffer_set_array_size (struct scratch_buffer *buffer,
fa3bfd
+			       size_t nelem, size_t size)
fa3bfd
+{
fa3bfd
+  return __glibc_likely (__libc_scratch_buffer_set_array_size
fa3bfd
+			 (buffer, nelem, size));
fa3bfd
+}
fa3bfd
+
fa3bfd
+#endif /* _SCRATCH_BUFFER_H */
fa3bfd
diff --git a/malloc/Makefile b/malloc/Makefile
fa3bfd
index 70e3cefa7b06157e..38aa9e0993d4880c 100644
fa3bfd
--- a/malloc/Makefile
fa3bfd
+++ b/malloc/Makefile
fa3bfd
@@ -33,6 +33,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
fa3bfd
 	 tst-interpose-thread \
fa3bfd
 	 tst-interpose-static-nothread \
fa3bfd
 	 tst-interpose-static-thread \
fa3bfd
+	 tst-scratch_buffer \
fa3bfd
 
fa3bfd
 tests-static := \
fa3bfd
 	 tst-interpose-static-nothread \
fa3bfd
@@ -40,7 +41,11 @@ tests-static := \
fa3bfd
 
fa3bfd
 test-srcs = tst-mtrace
fa3bfd
 
fa3bfd
-routines = malloc morecore mcheck mtrace obstack
fa3bfd
+routines = malloc morecore mcheck mtrace obstack \
fa3bfd
+  scratch_buffer_grow \
fa3bfd
+  scratch_buffer_grow_preserve \
fa3bfd
+  scratch_buffer_set_array_size \
fa3bfd
+
fa3bfd
 
fa3bfd
 install-lib := libmcheck.a
fa3bfd
 non-lib.a := libmcheck.a
fa3bfd
diff --git a/malloc/Versions b/malloc/Versions
fa3bfd
index 7ca9bdf25fcafdf2..f3c3d8a0934bdcd3 100644
fa3bfd
--- a/malloc/Versions
fa3bfd
+++ b/malloc/Versions
fa3bfd
@@ -67,5 +67,10 @@ libc {
fa3bfd
 
fa3bfd
     # Internal destructor hook for libpthread.
fa3bfd
     __libc_thread_freeres;
fa3bfd
+
fa3bfd
+    # struct scratch_buffer support
fa3bfd
+    __libc_scratch_buffer_grow;
fa3bfd
+    __libc_scratch_buffer_grow_preserve;
fa3bfd
+    __libc_scratch_buffer_set_array_size;
fa3bfd
   }
fa3bfd
 }
fa3bfd
diff --git a/malloc/scratch_buffer_grow.c b/malloc/scratch_buffer_grow.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..22bae506a1946eb4
fa3bfd
--- /dev/null
fa3bfd
+++ b/malloc/scratch_buffer_grow.c
fa3bfd
@@ -0,0 +1,52 @@
fa3bfd
+/* Variable-sized buffer with on-stack default allocation.
fa3bfd
+   Copyright (C) 2015-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <scratch_buffer.h>
fa3bfd
+#include <errno.h>
fa3bfd
+
fa3bfd
+bool
fa3bfd
+__libc_scratch_buffer_grow (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  void *new_ptr;
fa3bfd
+  size_t new_length = buffer->length * 2;
fa3bfd
+
fa3bfd
+  /* Discard old buffer.  */
fa3bfd
+  scratch_buffer_free (buffer);
fa3bfd
+
fa3bfd
+  /* Check for overflow.  */
fa3bfd
+  if (__glibc_likely (new_length >= buffer->length))
fa3bfd
+    new_ptr = malloc (new_length);
fa3bfd
+  else
fa3bfd
+    {
fa3bfd
+      __set_errno (ENOMEM);
fa3bfd
+      new_ptr = NULL;
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  if (__glibc_unlikely (new_ptr == NULL))
fa3bfd
+    {
fa3bfd
+      /* Buffer must remain valid to free.  */
fa3bfd
+      scratch_buffer_init (buffer);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  /* Install new heap-based buffer.  */
fa3bfd
+  buffer->data = new_ptr;
fa3bfd
+  buffer->length = new_length;
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+libc_hidden_def (__libc_scratch_buffer_grow);
fa3bfd
diff --git a/malloc/scratch_buffer_grow_preserve.c b/malloc/scratch_buffer_grow_preserve.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..18543ef85b7a48e1
fa3bfd
--- /dev/null
fa3bfd
+++ b/malloc/scratch_buffer_grow_preserve.c
fa3bfd
@@ -0,0 +1,63 @@
fa3bfd
+/* Variable-sized buffer with on-stack default allocation.
fa3bfd
+   Copyright (C) 2015-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <scratch_buffer.h>
fa3bfd
+#include <errno.h>
fa3bfd
+#include <string.h>
fa3bfd
+
fa3bfd
+bool
fa3bfd
+__libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
fa3bfd
+{
fa3bfd
+  size_t new_length = 2 * buffer->length;
fa3bfd
+  void *new_ptr;
fa3bfd
+
fa3bfd
+  if (buffer->data == buffer->__space)
fa3bfd
+    {
fa3bfd
+      /* Move buffer to the heap.  No overflow is possible because
fa3bfd
+	 buffer->length describes a small buffer on the stack.  */
fa3bfd
+      new_ptr = malloc (new_length);
fa3bfd
+      if (new_ptr == NULL)
fa3bfd
+	return false;
fa3bfd
+      memcpy (new_ptr, buffer->__space, buffer->length);
fa3bfd
+    }
fa3bfd
+  else
fa3bfd
+    {
fa3bfd
+      /* Buffer was already on the heap.  Check for overflow.  */
fa3bfd
+      if (__glibc_likely (new_length >= buffer->length))
fa3bfd
+	new_ptr = realloc (buffer->data, new_length);
fa3bfd
+      else
fa3bfd
+	{
fa3bfd
+	  __set_errno (ENOMEM);
fa3bfd
+	  new_ptr = NULL;
fa3bfd
+	}
fa3bfd
+
fa3bfd
+      if (__glibc_unlikely (new_ptr == NULL))
fa3bfd
+	{
fa3bfd
+	  /* Deallocate, but buffer must remain valid to free.  */
fa3bfd
+	  free (buffer->data);
fa3bfd
+	  scratch_buffer_init (buffer);
fa3bfd
+	  return false;
fa3bfd
+	}
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  /* Install new heap-based buffer.  */
fa3bfd
+  buffer->data = new_ptr;
fa3bfd
+  buffer->length = new_length;
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+libc_hidden_def (__libc_scratch_buffer_grow_preserve);
fa3bfd
diff --git a/malloc/scratch_buffer_set_array_size.c b/malloc/scratch_buffer_set_array_size.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..8ab6d9d300d5c10d
fa3bfd
--- /dev/null
fa3bfd
+++ b/malloc/scratch_buffer_set_array_size.c
fa3bfd
@@ -0,0 +1,60 @@
fa3bfd
+/* Variable-sized buffer with on-stack default allocation.
fa3bfd
+   Copyright (C) 2015-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <scratch_buffer.h>
fa3bfd
+#include <errno.h>
fa3bfd
+#include <limits.h>
fa3bfd
+
fa3bfd
+bool
fa3bfd
+__libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
fa3bfd
+				      size_t nelem, size_t size)
fa3bfd
+{
fa3bfd
+  size_t new_length = nelem * size;
fa3bfd
+
fa3bfd
+  /* Avoid overflow check if both values are small. */
fa3bfd
+  if ((nelem | size) >> (sizeof (size_t) * CHAR_BIT / 2) != 0
fa3bfd
+      && nelem != 0 && size != new_length / nelem)
fa3bfd
+    {
fa3bfd
+      /* Overflow.  Discard the old buffer, but it must remain valid
fa3bfd
+	 to free.  */
fa3bfd
+      scratch_buffer_free (buffer);
fa3bfd
+      scratch_buffer_init (buffer);
fa3bfd
+      __set_errno (ENOMEM);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  if (new_length <= buffer->length)
fa3bfd
+    return true;
fa3bfd
+
fa3bfd
+  /* Discard old buffer.  */
fa3bfd
+  scratch_buffer_free (buffer);
fa3bfd
+
fa3bfd
+  char *new_ptr = malloc (new_length);
fa3bfd
+  if (new_ptr == NULL)
fa3bfd
+    {
fa3bfd
+      /* Buffer must remain valid to free.  */
fa3bfd
+      scratch_buffer_init (buffer);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  /* Install new heap-based buffer.  */
fa3bfd
+  buffer->data = new_ptr;
fa3bfd
+  buffer->length = new_length;
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+libc_hidden_def (__libc_scratch_buffer_set_array_size);
fa3bfd
diff --git a/malloc/tst-scratch_buffer.c b/malloc/tst-scratch_buffer.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..5c9f3442ae8d6ef9
fa3bfd
--- /dev/null
fa3bfd
+++ b/malloc/tst-scratch_buffer.c
fa3bfd
@@ -0,0 +1,155 @@
fa3bfd
+/*
fa3bfd
+   Copyright (C) 2015-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <scratch_buffer.h>
fa3bfd
+#include <stdbool.h>
fa3bfd
+#include <stdio.h>
fa3bfd
+#include <string.h>
fa3bfd
+
fa3bfd
+static bool
fa3bfd
+unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
fa3bfd
+{
fa3bfd
+  size_t old_length = buf->length;
fa3bfd
+  if (!scratch_buffer_set_array_size (buf, a, b))
fa3bfd
+    {
fa3bfd
+      printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
fa3bfd
+	      a, b);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+  if (old_length != buf->length)
fa3bfd
+    {
fa3bfd
+      printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
fa3bfd
+	      a, b);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+
fa3bfd
+static bool
fa3bfd
+array_size_must_fail (size_t a, size_t b)
fa3bfd
+{
fa3bfd
+  for (int pass = 0; pass < 2; ++pass)
fa3bfd
+    {
fa3bfd
+      struct scratch_buffer buf;
fa3bfd
+      scratch_buffer_init (&buf;;
fa3bfd
+      if (pass > 0)
fa3bfd
+	if (!scratch_buffer_grow (&buf))
fa3bfd
+	  {
fa3bfd
+	    printf ("scratch_buffer_grow in array_size_must_fail failed\n");
fa3bfd
+	    return false;
fa3bfd
+	  }
fa3bfd
+      if (scratch_buffer_set_array_size (&buf, a, b))
fa3bfd
+	{
fa3bfd
+	  printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
fa3bfd
+		  pass, a, b);
fa3bfd
+	  return false;
fa3bfd
+	}
fa3bfd
+      if (buf.data != buf.__space)
fa3bfd
+	{
fa3bfd
+	  printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
fa3bfd
+		  pass, a, b);
fa3bfd
+	  return false;
fa3bfd
+	}
fa3bfd
+    }
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+
fa3bfd
+static int
fa3bfd
+do_test (void)
fa3bfd
+{
fa3bfd
+  {
fa3bfd
+    struct scratch_buffer buf;
fa3bfd
+    scratch_buffer_init (&buf;;
fa3bfd
+    memset (buf.data, ' ', buf.length);
fa3bfd
+    scratch_buffer_free (&buf;;
fa3bfd
+  }
fa3bfd
+  {
fa3bfd
+    struct scratch_buffer buf;
fa3bfd
+    scratch_buffer_init (&buf;;
fa3bfd
+    memset (buf.data, ' ', buf.length);
fa3bfd
+    size_t old_length = buf.length;
fa3bfd
+    scratch_buffer_grow (&buf;;
fa3bfd
+    if (buf.length <= old_length)
fa3bfd
+      {
fa3bfd
+	printf ("scratch_buffer_grow did not enlarge buffer\n");
fa3bfd
+	return 1;
fa3bfd
+      }
fa3bfd
+    memset (buf.data, ' ', buf.length);
fa3bfd
+    scratch_buffer_free (&buf;;
fa3bfd
+  }
fa3bfd
+  {
fa3bfd
+    struct scratch_buffer buf;
fa3bfd
+    scratch_buffer_init (&buf;;
fa3bfd
+    memset (buf.data, '@', buf.length);
fa3bfd
+    strcpy (buf.data, "prefix");
fa3bfd
+    size_t old_length = buf.length;
fa3bfd
+    scratch_buffer_grow_preserve (&buf;;
fa3bfd
+    if (buf.length <= old_length)
fa3bfd
+      {
fa3bfd
+	printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
fa3bfd
+	return 1;
fa3bfd
+      }
fa3bfd
+    if (strcmp (buf.data, "prefix") != 0)
fa3bfd
+      {
fa3bfd
+	printf ("scratch_buffer_grow_preserve did not copy buffer\n");
fa3bfd
+	return 1;
fa3bfd
+      }
fa3bfd
+    for (unsigned i = 7; i < old_length; ++i)
fa3bfd
+      if (((char *)buf.data)[i] != '@')
fa3bfd
+	{
fa3bfd
+	  printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
fa3bfd
+		  i);
fa3bfd
+	  return 1;
fa3bfd
+	}
fa3bfd
+    scratch_buffer_free (&buf;;
fa3bfd
+  }
fa3bfd
+  {
fa3bfd
+    struct scratch_buffer buf;
fa3bfd
+    scratch_buffer_init (&buf;;
fa3bfd
+    for (int pass = 0; pass < 4; ++pass)
fa3bfd
+      {
fa3bfd
+	if (!(unchanged_array_size (&buf, 0, 0)
fa3bfd
+	      && unchanged_array_size (&buf, 1, 0)
fa3bfd
+	      && unchanged_array_size (&buf, 0, 1)
fa3bfd
+	      && unchanged_array_size (&buf, -1, 0)
fa3bfd
+	      && unchanged_array_size (&buf, 0, -1)
fa3bfd
+	      && unchanged_array_size (&buf, 1ULL << 16, 0)
fa3bfd
+	      && unchanged_array_size (&buf, 0, 1ULL << 16)
fa3bfd
+	      && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
fa3bfd
+	      && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
fa3bfd
+	  return 1;
fa3bfd
+	if (!scratch_buffer_grow (&buf))
fa3bfd
+	  {
fa3bfd
+	    printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
fa3bfd
+	  }
fa3bfd
+      }
fa3bfd
+    scratch_buffer_free (&buf;;
fa3bfd
+  }
fa3bfd
+  {
fa3bfd
+    if (!(array_size_must_fail (-1, 1)
fa3bfd
+	  && array_size_must_fail (-1, -1)
fa3bfd
+	  && array_size_must_fail (1, -1)
fa3bfd
+	  && array_size_must_fail (((size_t)-1) / 4, 4)
fa3bfd
+	  && array_size_must_fail (4, ((size_t)-1) / 4)))
fa3bfd
+	return 1;
fa3bfd
+  }
fa3bfd
+  return 0;
fa3bfd
+}
fa3bfd
+
fa3bfd
+#define TEST_FUNCTION do_test ()
fa3bfd
+#include "../test-skeleton.c"