Blame SOURCES/glibc-rh677316-scratch_buffer.patch

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