51f0aa
Backport of the following upstream commits:
51f0aa
51f0aa
commit ab5ac271e6210fa0af11cf3ca525ce573bc47c48
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Wed Sep 6 11:25:14 2017 +0200
51f0aa
51f0aa
    __libc_dynarray_emplace_enlarge: Add missing else
51f0aa
    
51f0aa
    Before, arrays of small elements received a starting allocation size of
51f0aa
    8, not 16.
51f0aa
51f0aa
commit 5898f4548efdcd7c0fd437a74eeb80facc51a117
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Wed Aug 30 20:10:56 2017 +0200
51f0aa
51f0aa
    dynarray: Set errno on overflow-induced allocation failure
51f0aa
    
51f0aa
    This allows the caller to return directly on such an error, with an
51f0aa
    appropriate errno value.
51f0aa
51f0aa
commit 5b83faf6a7ca57ef2bfbca2c77992cafc8daa0be
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Mon Jun 19 12:58:08 2017 +0200
51f0aa
51f0aa
    dynarray: Use libc_hidden_proto only for !_ISOMAC
51f0aa
51f0aa
    With this change, it is possible to use dynarray from non-internal
51f0aa
    tests.
51f0aa
51f0aa
commit f8bf87face3304f216bcd838081fa33bb4976ac6
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Tue Jun 13 17:03:56 2017 +0200
51f0aa
51f0aa
    dynarray: Implement begin/end functions in the spirit of C++
51f0aa
51f0aa
commit 990c32b93a29d8b8d599e10ebca19a260f84cbba
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Fri Jun 9 14:08:57 2017 +0200
51f0aa
51f0aa
    malloc: Remove tst-dynarray, tst-dynarray-fail from test-srcs
51f0aa
    
51f0aa
    They are already covered through the tests variable.
51f0aa
51f0aa
commit 91b6eb1140eda6bab324821ee3785e5d0ca155b8
51f0aa
Author: Florian Weimer <fweimer@redhat.com>
51f0aa
Date:   Fri Jun 2 11:59:28 2017 +0200
51f0aa
51f0aa
    Add internal facility for dynamic array handling
51f0aa
51f0aa
    This is intended as a type-safe alternative to obstacks and
51f0aa
    hand-written realloc constructs.  The implementation avoids
51f0aa
    writing function pointers to the heap.
51f0aa
51f0aa
malloc/tst-dynarray-at-fail.c has been adjusted to use __WORDSIZE
51f0aa
instead of SIZE_WIDTH.
51f0aa
51f0aa
malloc/Makefile has been adjusted to the changes in the build and test
51f0aa
process (no tests-internal in glibc 2.17, mtrace tests are handled
51f0aa
differently, there is no evaluate-test Makefile macro).
51f0aa
51f0aa
diff --git a/malloc/Makefile b/malloc/Makefile
51f0aa
index 38aa9e0993d4880c..63fc3291dcc4077a 100644
51f0aa
--- a/malloc/Makefile
51f0aa
+++ b/malloc/Makefile
51f0aa
@@ -34,6 +34,9 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
51f0aa
 	 tst-interpose-static-nothread \
51f0aa
 	 tst-interpose-static-thread \
51f0aa
 	 tst-scratch_buffer \
51f0aa
+	 tst-dynarray \
51f0aa
+	 tst-dynarray-fail \
51f0aa
+	 tst-dynarray-at-fail \
51f0aa
 
51f0aa
 tests-static := \
51f0aa
 	 tst-interpose-static-nothread \
51f0aa
@@ -41,10 +44,17 @@ tests-static := \
51f0aa
 
51f0aa
 test-srcs = tst-mtrace
51f0aa
 
51f0aa
+generated += tst-dynarray-mem tst-dynarray-fail-mem
51f0aa
+
51f0aa
 routines = malloc morecore mcheck mtrace obstack \
51f0aa
   scratch_buffer_grow \
51f0aa
   scratch_buffer_grow_preserve \
51f0aa
   scratch_buffer_set_array_size \
51f0aa
+  dynarray_at_failure \
51f0aa
+  dynarray_emplace_enlarge \
51f0aa
+  dynarray_finalize \
51f0aa
+  dynarray_resize \
51f0aa
+  dynarray_resize_clear \
51f0aa
 
51f0aa
 
51f0aa
 install-lib := libmcheck.a
51f0aa
@@ -143,8 +153,7 @@ ifeq ($(run-built-tests),yes)
51f0aa
 ifeq (yes,$(build-shared))
51f0aa
 ifneq ($(PERL),no)
51f0aa
 tests: $(objpfx)tst-mtrace.out
51f0aa
-$(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
51f0aa
-	$(SHELL) $< $(common-objpfx) '$(run-program-prefix)'
51f0aa
+tests: $(objpfx)tst-dynarray-mem $(objpfx)tst-dynarray-fail-mem
51f0aa
 endif
51f0aa
 endif
51f0aa
 endif
51f0aa
@@ -194,3 +203,11 @@ $(objpfx)tst-interpose-static-thread: \
51f0aa
 # Compile the tests with a flag which suppresses the mallopt call in
51f0aa
 # the test skeleton.
51f0aa
 $(tests:%=$(objpfx)%.o): CPPFLAGS += -DTEST_NO_MALLOPT
51f0aa
+
51f0aa
+tst-dynarray-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray.mtrace
51f0aa
+$(objpfx)tst-dynarray-mem: $(objpfx)tst-dynarray.out
51f0aa
+	$(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray.mtrace > $@
51f0aa
+
51f0aa
+tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace
51f0aa
+$(objpfx)tst-dynarray-fail-mem: $(objpfx)tst-dynarray-fail.out
51f0aa
+	$(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@
51f0aa
diff --git a/malloc/Versions b/malloc/Versions
51f0aa
index f3c3d8a0934bdcd3..16f9dab418a4e3f6 100644
51f0aa
--- a/malloc/Versions
51f0aa
+++ b/malloc/Versions
51f0aa
@@ -72,5 +72,12 @@ libc {
51f0aa
     __libc_scratch_buffer_grow;
51f0aa
     __libc_scratch_buffer_grow_preserve;
51f0aa
     __libc_scratch_buffer_set_array_size;
51f0aa
+
51f0aa
+    # dynarray support
51f0aa
+    __libc_dynarray_at_failure;
51f0aa
+    __libc_dynarray_emplace_enlarge;
51f0aa
+    __libc_dynarray_finalize;
51f0aa
+    __libc_dynarray_resize;
51f0aa
+    __libc_dynarray_resize_clear;
51f0aa
   }
51f0aa
 }
51f0aa
diff --git a/malloc/dynarray-skeleton.c b/malloc/dynarray-skeleton.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..7ec58788087e80d3
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray-skeleton.c
51f0aa
@@ -0,0 +1,521 @@
51f0aa
+/* Type-safe arrays which grow dynamically.
51f0aa
+   Copyright (C) 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
+/* Pre-processor macros which act as parameters:
51f0aa
+
51f0aa
+   DYNARRAY_STRUCT
51f0aa
+      The struct tag of dynamic array to be defined.
51f0aa
+   DYNARRAY_ELEMENT
51f0aa
+      The type name of the element type.  Elements are copied
51f0aa
+      as if by memcpy, and can change address as the dynamic
51f0aa
+      array grows.
51f0aa
+   DYNARRAY_PREFIX
51f0aa
+      The prefix of the functions which are defined.
51f0aa
+
51f0aa
+   The following parameters are optional:
51f0aa
+
51f0aa
+   DYNARRAY_ELEMENT_FREE
51f0aa
+      DYNARRAY_ELEMENT_FREE (E) is evaluated to deallocate the
51f0aa
+      contents of elements. E is of type  DYNARRAY_ELEMENT *.
51f0aa
+   DYNARRAY_ELEMENT_INIT
51f0aa
+      DYNARRAY_ELEMENT_INIT (E) is evaluated to initialize a new
51f0aa
+      element.  E is of type  DYNARRAY_ELEMENT *.
51f0aa
+      If DYNARRAY_ELEMENT_FREE but not DYNARRAY_ELEMENT_INIT is
51f0aa
+      defined, new elements are automatically zero-initialized.
51f0aa
+      Otherwise, new elements have undefined contents.
51f0aa
+   DYNARRAY_INITIAL_SIZE
51f0aa
+      The size of the statically allocated array (default:
51f0aa
+      at least 2, more elements if they fit into 128 bytes).
51f0aa
+      Must be a preprocessor constant.  If DYNARRAY_INITIAL_SIZE is 0,
51f0aa
+      there is no statically allocated array at, and all non-empty
51f0aa
+      arrays are heap-allocated.
51f0aa
+   DYNARRAY_FINAL_TYPE
51f0aa
+      The name of the type which holds the final array.  If not
51f0aa
+      defined, is PREFIX##finalize not provided.  DYNARRAY_FINAL_TYPE
51f0aa
+      must be a struct type, with members of type DYNARRAY_ELEMENT and
51f0aa
+      size_t at the start (in this order).
51f0aa
+
51f0aa
+   These macros are undefined after this header file has been
51f0aa
+   included.
51f0aa
+
51f0aa
+   The following types are provided (their members are private to the
51f0aa
+   dynarray implementation):
51f0aa
+
51f0aa
+     struct DYNARRAY_STRUCT
51f0aa
+
51f0aa
+   The following functions are provided:
51f0aa
+
51f0aa
+     void DYNARRAY_PREFIX##init (struct DYNARRAY_STRUCT *);
51f0aa
+     void DYNARRAY_PREFIX##free (struct DYNARRAY_STRUCT *);
51f0aa
+     bool DYNARRAY_PREFIX##has_failed (const struct DYNARRAY_STRUCT *);
51f0aa
+     void DYNARRAY_PREFIX##mark_failed (struct DYNARRAY_STRUCT *);
51f0aa
+     size_t DYNARRAY_PREFIX##size (const struct DYNARRAY_STRUCT *);
51f0aa
+     DYNARRAY_ELEMENT *DYNARRAY_PREFIX##begin (const struct DYNARRAY_STRUCT *);
51f0aa
+     DYNARRAY_ELEMENT *DYNARRAY_PREFIX##end (const struct DYNARRAY_STRUCT *);
51f0aa
+     DYNARRAY_ELEMENT *DYNARRAY_PREFIX##at (struct DYNARRAY_STRUCT *, size_t);
51f0aa
+     void DYNARRAY_PREFIX##add (struct DYNARRAY_STRUCT *, DYNARRAY_ELEMENT);
51f0aa
+     DYNARRAY_ELEMENT *DYNARRAY_PREFIX##emplace (struct DYNARRAY_STRUCT *);
51f0aa
+     bool DYNARRAY_PREFIX##resize (struct DYNARRAY_STRUCT *, size_t);
51f0aa
+     void DYNARRAY_PREFIX##remove_last (struct DYNARRAY_STRUCT *);
51f0aa
+     void DYNARRAY_PREFIX##clear (struct DYNARRAY_STRUCT *);
51f0aa
+
51f0aa
+   The following functions are provided are provided if the
51f0aa
+   prerequisites are met:
51f0aa
+
51f0aa
+     bool DYNARRAY_PREFIX##finalize (struct DYNARRAY_STRUCT *,
51f0aa
+                                     DYNARRAY_FINAL_TYPE *);
51f0aa
+       (if DYNARRAY_FINAL_TYPE is defined)
51f0aa
+     DYNARRAY_ELEMENT *DYNARRAY_PREFIX##finalize (struct DYNARRAY_STRUCT *,
51f0aa
+                                                  size_t *);
51f0aa
+       (if DYNARRAY_FINAL_TYPE is not defined)
51f0aa
+*/
51f0aa
+
51f0aa
+#include <malloc/dynarray.h>
51f0aa
+
51f0aa
+#include <errno.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+#ifndef DYNARRAY_STRUCT
51f0aa
+# error "DYNARRAY_STRUCT must be defined"
51f0aa
+#endif
51f0aa
+
51f0aa
+#ifndef DYNARRAY_ELEMENT
51f0aa
+# error "DYNARRAY_ELEMENT must be defined"
51f0aa
+#endif
51f0aa
+
51f0aa
+#ifndef DYNARRAY_PREFIX
51f0aa
+# error "DYNARRAY_PREFIX must be defined"
51f0aa
+#endif
51f0aa
+
51f0aa
+#ifdef DYNARRAY_INITIAL_SIZE
51f0aa
+# if DYNARRAY_INITIAL_SIZE < 0
51f0aa
+#  error "DYNARRAY_INITIAL_SIZE must be non-negative"
51f0aa
+# endif
51f0aa
+# if DYNARRAY_INITIAL_SIZE > 0
51f0aa
+#  define DYNARRAY_HAVE_SCRATCH 1
51f0aa
+# else
51f0aa
+#  define DYNARRAY_HAVE_SCRATCH 0
51f0aa
+# endif
51f0aa
+#else
51f0aa
+/* Provide a reasonable default which limits the size of
51f0aa
+   DYNARRAY_STRUCT.  */
51f0aa
+# define DYNARRAY_INITIAL_SIZE \
51f0aa
+  (sizeof (DYNARRAY_ELEMENT) > 64 ? 2 : 128 / sizeof (DYNARRAY_ELEMENT))
51f0aa
+# define DYNARRAY_HAVE_SCRATCH 1
51f0aa
+#endif
51f0aa
+
51f0aa
+/* Public type definitions.  */
51f0aa
+
51f0aa
+/* All fields of this struct are private to the implementation.  */
51f0aa
+struct DYNARRAY_STRUCT
51f0aa
+{
51f0aa
+  union
51f0aa
+  {
51f0aa
+    struct dynarray_header dynarray_abstract;
51f0aa
+    struct
51f0aa
+    {
51f0aa
+      /* These fields must match struct dynarray_header.  */
51f0aa
+      size_t used;
51f0aa
+      size_t allocated;
51f0aa
+      DYNARRAY_ELEMENT *array;
51f0aa
+    } dynarray_header;
51f0aa
+  };
51f0aa
+
51f0aa
+#if DYNARRAY_HAVE_SCRATCH
51f0aa
+  /* Initial inline allocation.  */
51f0aa
+  DYNARRAY_ELEMENT scratch[DYNARRAY_INITIAL_SIZE];
51f0aa
+#endif
51f0aa
+};
51f0aa
+
51f0aa
+/* Internal use only: Helper macros.  */
51f0aa
+
51f0aa
+/* Ensure macro-expansion of DYNARRAY_PREFIX.  */
51f0aa
+#define DYNARRAY_CONCAT0(prefix, name) prefix##name
51f0aa
+#define DYNARRAY_CONCAT1(prefix, name) DYNARRAY_CONCAT0(prefix, name)
51f0aa
+#define DYNARRAY_NAME(name) DYNARRAY_CONCAT1(DYNARRAY_PREFIX, name)
51f0aa
+
51f0aa
+/* Address of the scratch buffer if any.  */
51f0aa
+#if DYNARRAY_HAVE_SCRATCH
51f0aa
+# define DYNARRAY_SCRATCH(list) (list)->scratch
51f0aa
+#else
51f0aa
+# define DYNARRAY_SCRATCH(list) NULL
51f0aa
+#endif
51f0aa
+
51f0aa
+/* Internal use only: Helper functions.  */
51f0aa
+
51f0aa
+/* Internal function.  Call DYNARRAY_ELEMENT_FREE with the array
51f0aa
+   elements.  Name mangling needed due to the DYNARRAY_ELEMENT_FREE
51f0aa
+   macro expansion.  */
51f0aa
+static inline void
51f0aa
+DYNARRAY_NAME (free__elements__) (DYNARRAY_ELEMENT *__dynarray_array,
51f0aa
+                                  size_t __dynarray_used)
51f0aa
+{
51f0aa
+#ifdef DYNARRAY_ELEMENT_FREE
51f0aa
+  for (size_t __dynarray_i = 0; __dynarray_i < __dynarray_used; ++__dynarray_i)
51f0aa
+    DYNARRAY_ELEMENT_FREE (&__dynarray_array[__dynarray_i]);
51f0aa
+#endif /* DYNARRAY_ELEMENT_FREE */
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  Free the non-scratch array allocation.  */
51f0aa
+static inline void
51f0aa
+DYNARRAY_NAME (free__array__) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+#if DYNARRAY_HAVE_SCRATCH
51f0aa
+  if (list->dynarray_header.array != list->scratch)
51f0aa
+    free (list->dynarray_header.array);
51f0aa
+#else
51f0aa
+  free (list->dynarray_header.array);
51f0aa
+#endif
51f0aa
+}
51f0aa
+
51f0aa
+/* Public functions.  */
51f0aa
+
51f0aa
+/* Initialize a dynamic array object.  This must be called before any
51f0aa
+   use of the object.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (init) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  list->dynarray_header.used = 0;
51f0aa
+  list->dynarray_header.allocated = DYNARRAY_INITIAL_SIZE;
51f0aa
+  list->dynarray_header.array = DYNARRAY_SCRATCH (list);
51f0aa
+}
51f0aa
+
51f0aa
+/* Deallocate the dynamic array and its elements.  */
51f0aa
+__attribute__ ((unused, nonnull (1)))
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (free) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  DYNARRAY_NAME (free__elements__)
51f0aa
+    (list->dynarray_header.array, list->dynarray_header.used);
51f0aa
+  DYNARRAY_NAME (free__array__) (list);
51f0aa
+  DYNARRAY_NAME (init) (list);
51f0aa
+}
51f0aa
+
51f0aa
+/* Return true if the dynamic array is in an error state.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static inline bool
51f0aa
+DYNARRAY_NAME (has_failed) (const struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  return list->dynarray_header.allocated == __dynarray_error_marker ();
51f0aa
+}
51f0aa
+
51f0aa
+/* Mark the dynamic array as failed.  All elements are deallocated as
51f0aa
+   a side effect.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (mark_failed) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  DYNARRAY_NAME (free__elements__)
51f0aa
+    (list->dynarray_header.array, list->dynarray_header.used);
51f0aa
+  DYNARRAY_NAME (free__array__) (list);
51f0aa
+  list->dynarray_header.array = DYNARRAY_SCRATCH (list);
51f0aa
+  list->dynarray_header.used = 0;
51f0aa
+  list->dynarray_header.allocated = __dynarray_error_marker ();
51f0aa
+}
51f0aa
+
51f0aa
+/* Return the number of elements which have been added to the dynamic
51f0aa
+   array.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static inline size_t
51f0aa
+DYNARRAY_NAME (size) (const struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  return list->dynarray_header.used;
51f0aa
+}
51f0aa
+
51f0aa
+/* Return a pointer to the array element at INDEX.  Terminate the
51f0aa
+   process if INDEX is out of bounds.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static inline DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (at) (struct DYNARRAY_STRUCT *list, size_t index)
51f0aa
+{
51f0aa
+  if (__glibc_unlikely (index >= DYNARRAY_NAME (size) (list)))
51f0aa
+    __libc_dynarray_at_failure (DYNARRAY_NAME (size) (list), index);
51f0aa
+  return list->dynarray_header.array + index;
51f0aa
+}
51f0aa
+
51f0aa
+/* Return a pointer to the first array element, if any.  For a
51f0aa
+   zero-length array, the pointer can be NULL even though the dynamic
51f0aa
+   array has not entered the failure state.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static inline DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (begin) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  return list->dynarray_header.array;
51f0aa
+}
51f0aa
+
51f0aa
+/* Return a pointer one element past the last array element.  For a
51f0aa
+   zero-length array, the pointer can be NULL even though the dynamic
51f0aa
+   array has not entered the failure state.  */
51f0aa
+__attribute__ ((nonnull (1)))
51f0aa
+static inline DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (end) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  return list->dynarray_header.array + list->dynarray_header.used;
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  Slow path for the add function below.  */
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (add__) (struct DYNARRAY_STRUCT *list, DYNARRAY_ELEMENT item)
51f0aa
+{
51f0aa
+  if (__glibc_unlikely
51f0aa
+      (!__libc_dynarray_emplace_enlarge (&list->dynarray_abstract,
51f0aa
+                                         DYNARRAY_SCRATCH (list),
51f0aa
+                                         sizeof (DYNARRAY_ELEMENT))))
51f0aa
+    {
51f0aa
+      DYNARRAY_NAME (mark_failed) (list);
51f0aa
+      return;
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Copy the new element and increase the array length.  */
51f0aa
+  list->dynarray_header.array[list->dynarray_header.used++] = item;
51f0aa
+}
51f0aa
+
51f0aa
+/* Add ITEM at the end of the array, enlarging it by one element.
51f0aa
+   Mark *LIST as failed if the dynamic array allocation size cannot be
51f0aa
+   increased.  */
51f0aa
+__attribute__ ((unused, nonnull (1)))
51f0aa
+static inline void
51f0aa
+DYNARRAY_NAME (add) (struct DYNARRAY_STRUCT *list, DYNARRAY_ELEMENT item)
51f0aa
+{
51f0aa
+  /* Do nothing in case of previous error.  */
51f0aa
+  if (DYNARRAY_NAME (has_failed) (list))
51f0aa
+    return;
51f0aa
+
51f0aa
+  /* Enlarge the array if necessary.  */
51f0aa
+  if (__glibc_unlikely (list->dynarray_header.used
51f0aa
+                        == list->dynarray_header.allocated))
51f0aa
+    {
51f0aa
+      DYNARRAY_NAME (add__) (list, item);
51f0aa
+      return;
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Copy the new element and increase the array length.  */
51f0aa
+  list->dynarray_header.array[list->dynarray_header.used++] = item;
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  Building block for the emplace functions below.
51f0aa
+   Assumes space for one more element in *LIST.  */
51f0aa
+static inline DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (emplace__tail__) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  DYNARRAY_ELEMENT *result
51f0aa
+    = &list->dynarray_header.array[list->dynarray_header.used];
51f0aa
+  ++list->dynarray_header.used;
51f0aa
+#if defined (DYNARRAY_ELEMENT_INIT)
51f0aa
+  DYNARRAY_ELEMENT_INIT (result);
51f0aa
+#elif defined (DYNARRAY_ELEMENT_FREE)
51f0aa
+  memset (result, 0, sizeof (*result));
51f0aa
+#endif
51f0aa
+  return result;
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  Slow path for the emplace function below.  */
51f0aa
+static DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (emplace__) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  if (__glibc_unlikely
51f0aa
+      (!__libc_dynarray_emplace_enlarge (&list->dynarray_abstract,
51f0aa
+                                         DYNARRAY_SCRATCH (list),
51f0aa
+                                         sizeof (DYNARRAY_ELEMENT))))
51f0aa
+    {
51f0aa
+      DYNARRAY_NAME (mark_failed) (list);
51f0aa
+      return NULL;
51f0aa
+    }
51f0aa
+  return DYNARRAY_NAME (emplace__tail__) (list);
51f0aa
+}
51f0aa
+
51f0aa
+/* Allocate a place for a new element in *LIST and return a pointer to
51f0aa
+   it.  The pointer can be NULL if the dynamic array cannot be
51f0aa
+   enlarged due to a memory allocation failure.  */
51f0aa
+__attribute__ ((unused, warn_unused_result, nonnull (1)))
51f0aa
+static
51f0aa
+/* Avoid inlining with the larger initialization code.  */
51f0aa
+#if !(defined (DYNARRAY_ELEMENT_INIT) || defined (DYNARRAY_ELEMENT_FREE))
51f0aa
+inline
51f0aa
+#endif
51f0aa
+DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (emplace) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  /* Do nothing in case of previous error.  */
51f0aa
+  if (DYNARRAY_NAME (has_failed) (list))
51f0aa
+    return NULL;
51f0aa
+
51f0aa
+  /* Enlarge the array if necessary.  */
51f0aa
+  if (__glibc_unlikely (list->dynarray_header.used
51f0aa
+                        == list->dynarray_header.allocated))
51f0aa
+    return (DYNARRAY_NAME (emplace__) (list));
51f0aa
+  return DYNARRAY_NAME (emplace__tail__) (list);
51f0aa
+}
51f0aa
+
51f0aa
+/* Change the size of *LIST to SIZE.  If SIZE is larger than the
51f0aa
+   existing size, new elements are added (which can be initialized).
51f0aa
+   Otherwise, the list is truncated, and elements are freed.  Return
51f0aa
+   false on memory allocation failure (and mark *LIST as failed).  */
51f0aa
+__attribute__ ((unused, nonnull (1)))
51f0aa
+static bool
51f0aa
+DYNARRAY_NAME (resize) (struct DYNARRAY_STRUCT *list, size_t size)
51f0aa
+{
51f0aa
+  if (size > list->dynarray_header.used)
51f0aa
+    {
51f0aa
+      bool ok;
51f0aa
+#if defined (DYNARRAY_ELEMENT_INIT)
51f0aa
+      /* The new elements have to be initialized.  */
51f0aa
+      size_t old_size = list->dynarray_header.used;
51f0aa
+      ok = __libc_dynarray_resize (&list->dynarray_abstract,
51f0aa
+                                   size, DYNARRAY_SCRATCH (list),
51f0aa
+                                   sizeof (DYNARRAY_ELEMENT));
51f0aa
+      if (ok)
51f0aa
+        for (size_t i = old_size; i < size; ++i)
51f0aa
+          {
51f0aa
+            DYNARRAY_ELEMENT_INIT (&list->dynarray_header.array[i]);
51f0aa
+          }
51f0aa
+#elif defined (DYNARRAY_ELEMENT_FREE)
51f0aa
+      /* Zero initialization is needed so that the elements can be
51f0aa
+         safely freed.  */
51f0aa
+      ok = __libc_dynarray_resize_clear
51f0aa
+        (&list->dynarray_abstract, size,
51f0aa
+         DYNARRAY_SCRATCH (list), sizeof (DYNARRAY_ELEMENT));
51f0aa
+#else
51f0aa
+      ok =  __libc_dynarray_resize (&list->dynarray_abstract,
51f0aa
+                                    size, DYNARRAY_SCRATCH (list),
51f0aa
+                                    sizeof (DYNARRAY_ELEMENT));
51f0aa
+#endif
51f0aa
+      if (__glibc_unlikely (!ok))
51f0aa
+        DYNARRAY_NAME (mark_failed) (list);
51f0aa
+      return ok;
51f0aa
+    }
51f0aa
+  else
51f0aa
+    {
51f0aa
+      /* The list has shrunk in size.  Free the removed elements.  */
51f0aa
+      DYNARRAY_NAME (free__elements__)
51f0aa
+        (list->dynarray_header.array + size,
51f0aa
+         list->dynarray_header.used - size);
51f0aa
+      list->dynarray_header.used = size;
51f0aa
+      return true;
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Remove the last element of LIST if it is present.  */
51f0aa
+__attribute__ ((unused, nonnull (1)))
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (remove_last) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  /* used > 0 implies that the array is the non-failed state.  */
51f0aa
+  if (list->dynarray_header.used > 0)
51f0aa
+    {
51f0aa
+      size_t new_length = list->dynarray_header.used - 1;
51f0aa
+#ifdef DYNARRAY_ELEMENT_FREE
51f0aa
+      DYNARRAY_ELEMENT_FREE (&list->dynarray_header.array[new_length]);
51f0aa
+#endif
51f0aa
+      list->dynarray_header.used = new_length;
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Remove all elements from the list.  The elements are freed, but the
51f0aa
+   list itself is not.  */
51f0aa
+__attribute__ ((unused, nonnull (1)))
51f0aa
+static void
51f0aa
+DYNARRAY_NAME (clear) (struct DYNARRAY_STRUCT *list)
51f0aa
+{
51f0aa
+  /* free__elements__ does nothing if the list is in the failed
51f0aa
+     state.  */
51f0aa
+  DYNARRAY_NAME (free__elements__)
51f0aa
+    (list->dynarray_header.array, list->dynarray_header.used);
51f0aa
+  list->dynarray_header.used = 0;
51f0aa
+}
51f0aa
+
51f0aa
+#ifdef DYNARRAY_FINAL_TYPE
51f0aa
+/* Transfer the dynamic array to a permanent location at *RESULT.
51f0aa
+   Returns true on success on false on allocation failure.  In either
51f0aa
+   case, *LIST is re-initialized and can be reused.  A NULL pointer is
51f0aa
+   stored in *RESULT if LIST refers to an empty list.  On success, the
51f0aa
+   pointer in *RESULT is heap-allocated and must be deallocated using
51f0aa
+   free.  */
51f0aa
+__attribute__ ((unused, warn_unused_result, nonnull (1, 2)))
51f0aa
+static bool
51f0aa
+DYNARRAY_NAME (finalize) (struct DYNARRAY_STRUCT *list,
51f0aa
+                          DYNARRAY_FINAL_TYPE *result)
51f0aa
+{
51f0aa
+  struct dynarray_finalize_result res;
51f0aa
+  if (__libc_dynarray_finalize (&list->dynarray_abstract,
51f0aa
+                                DYNARRAY_SCRATCH (list),
51f0aa
+                                sizeof (DYNARRAY_ELEMENT), &res))
51f0aa
+    {
51f0aa
+      /* On success, the result owns all the data.  */
51f0aa
+      DYNARRAY_NAME (init) (list);
51f0aa
+      *result = (DYNARRAY_FINAL_TYPE) { res.array, res.length };
51f0aa
+      return true;
51f0aa
+    }
51f0aa
+  else
51f0aa
+    {
51f0aa
+      /* On error, we need to free all data.  */
51f0aa
+      DYNARRAY_NAME (free) (list);
51f0aa
+      errno = ENOMEM;
51f0aa
+      return false;
51f0aa
+    }
51f0aa
+}
51f0aa
+#else /* !DYNARRAY_FINAL_TYPE */
51f0aa
+/* Transfer the dynamic array to a heap-allocated array and return a
51f0aa
+   pointer to it.  The pointer is NULL if memory allocation fails, or
51f0aa
+   if the array is empty, so this function should be used only for
51f0aa
+   arrays which are known not be empty (usually because they always
51f0aa
+   have a sentinel at the end).  If LENGTHP is not NULL, the array
51f0aa
+   length is written to *LENGTHP.  *LIST is re-initialized and can be
51f0aa
+   reused.  */
51f0aa
+__attribute__ ((unused, warn_unused_result, nonnull (1)))
51f0aa
+static DYNARRAY_ELEMENT *
51f0aa
+DYNARRAY_NAME (finalize) (struct DYNARRAY_STRUCT *list, size_t *lengthp)
51f0aa
+{
51f0aa
+  struct dynarray_finalize_result res;
51f0aa
+  if (__libc_dynarray_finalize (&list->dynarray_abstract,
51f0aa
+                                DYNARRAY_SCRATCH (list),
51f0aa
+                                sizeof (DYNARRAY_ELEMENT), &res))
51f0aa
+    {
51f0aa
+      /* On success, the result owns all the data.  */
51f0aa
+      DYNARRAY_NAME (init) (list);
51f0aa
+      if (lengthp != NULL)
51f0aa
+        *lengthp = res.length;
51f0aa
+      return res.array;
51f0aa
+    }
51f0aa
+  else
51f0aa
+    {
51f0aa
+      /* On error, we need to free all data.  */
51f0aa
+      DYNARRAY_NAME (free) (list);
51f0aa
+      errno = ENOMEM;
51f0aa
+      return NULL;
51f0aa
+    }
51f0aa
+}
51f0aa
+#endif /* !DYNARRAY_FINAL_TYPE */
51f0aa
+
51f0aa
+/* Undo macro definitions.  */
51f0aa
+
51f0aa
+#undef DYNARRAY_CONCAT0
51f0aa
+#undef DYNARRAY_CONCAT1
51f0aa
+#undef DYNARRAY_NAME
51f0aa
+#undef DYNARRAY_SCRATCH
51f0aa
+#undef DYNARRAY_HAVE_SCRATCH
51f0aa
+
51f0aa
+#undef DYNARRAY_STRUCT
51f0aa
+#undef DYNARRAY_ELEMENT
51f0aa
+#undef DYNARRAY_PREFIX
51f0aa
+#undef DYNARRAY_ELEMENT_FREE
51f0aa
+#undef DYNARRAY_ELEMENT_INIT
51f0aa
+#undef DYNARRAY_INITIAL_SIZE
51f0aa
+#undef DYNARRAY_FINAL_TYPE
51f0aa
diff --git a/malloc/dynarray.h b/malloc/dynarray.h
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..5888bcbc1d4ae9bf
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray.h
51f0aa
@@ -0,0 +1,179 @@
51f0aa
+/* Type-safe arrays which grow dynamically.  Shared definitions.
51f0aa
+   Copyright (C) 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
+/* To use the dynarray facility, you need to include
51f0aa
+   <malloc/dynarray-skeleton.c> and define the parameter macros
51f0aa
+   documented in that file.
51f0aa
+
51f0aa
+   A minimal example which provides a growing list of integers can be
51f0aa
+   defined like this:
51f0aa
+
51f0aa
+     struct int_array
51f0aa
+     {
51f0aa
+       // Pointer to result array followed by its length,
51f0aa
+       // as required by DYNARRAY_FINAL_TYPE.
51f0aa
+       int *array;
51f0aa
+       size_t length;
51f0aa
+     };
51f0aa
+
51f0aa
+     #define DYNARRAY_STRUCT dynarray_int
51f0aa
+     #define DYNARRAY_ELEMENT int
51f0aa
+     #define DYNARRAY_PREFIX dynarray_int_
51f0aa
+     #define DYNARRAY_FINAL_TYPE struct int_array
51f0aa
+     #include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+   To create a three-element array with elements 1, 2, 3, use this
51f0aa
+   code:
51f0aa
+
51f0aa
+     struct dynarray_int dyn;
51f0aa
+     dynarray_int_init (&dyn);
51f0aa
+     for (int i = 1; i <= 3; ++i)
51f0aa
+       {
51f0aa
+         int *place = dynarray_int_emplace (&dyn);
51f0aa
+         assert (place != NULL);
51f0aa
+         *place = i;
51f0aa
+       }
51f0aa
+     struct int_array result;
51f0aa
+     bool ok = dynarray_int_finalize (&dyn, &result);
51f0aa
+     assert (ok);
51f0aa
+     assert (result.length == 3);
51f0aa
+     assert (result.array[0] == 1);
51f0aa
+     assert (result.array[1] == 2);
51f0aa
+     assert (result.array[2] == 3);
51f0aa
+     free (result.array);
51f0aa
+
51f0aa
+   If the elements contain resources which must be freed, define
51f0aa
+   DYNARRAY_ELEMENT_FREE appropriately, like this:
51f0aa
+
51f0aa
+     struct str_array
51f0aa
+     {
51f0aa
+       char **array;
51f0aa
+       size_t length;
51f0aa
+     };
51f0aa
+
51f0aa
+     #define DYNARRAY_STRUCT dynarray_str
51f0aa
+     #define DYNARRAY_ELEMENT char *
51f0aa
+     #define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr)
51f0aa
+     #define DYNARRAY_PREFIX dynarray_str_
51f0aa
+     #define DYNARRAY_FINAL_TYPE struct str_array
51f0aa
+     #include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+   Compared to scratch buffers, dynamic arrays have the following
51f0aa
+   features:
51f0aa
+
51f0aa
+   - They have an element type, and are not just an untyped buffer of
51f0aa
+     bytes.
51f0aa
+
51f0aa
+   - When growing, previously stored elements are preserved.  (It is
51f0aa
+     expected that scratch_buffer_grow_preserve and
51f0aa
+     scratch_buffer_set_array_size eventually go away because all
51f0aa
+     current users are moved to dynamic arrays.)
51f0aa
+
51f0aa
+   - Scratch buffers have a more aggressive growth policy because
51f0aa
+     growing them typically means a retry of an operation (across an
51f0aa
+     NSS service module boundary), which is expensive.
51f0aa
+
51f0aa
+   - For the same reason, scratch buffers have a much larger initial
51f0aa
+     stack allocation.  */
51f0aa
+
51f0aa
+#ifndef _DYNARRAY_H
51f0aa
+#define _DYNARRAY_H
51f0aa
+
51f0aa
+#include <stdbool.h>
51f0aa
+#include <stddef.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+struct dynarray_header
51f0aa
+{
51f0aa
+  size_t used;
51f0aa
+  size_t allocated;
51f0aa
+  void *array;
51f0aa
+};
51f0aa
+
51f0aa
+/* Marker used in the allocated member to indicate that an error was
51f0aa
+   encountered.  */
51f0aa
+static inline size_t
51f0aa
+__dynarray_error_marker (void)
51f0aa
+{
51f0aa
+  return -1;
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  See the has_failed function in
51f0aa
+   dynarray-skeleton.c.  */
51f0aa
+static inline bool
51f0aa
+__dynarray_error (struct dynarray_header *list)
51f0aa
+{
51f0aa
+  return list->allocated == __dynarray_error_marker ();
51f0aa
+}
51f0aa
+
51f0aa
+/* Internal function.  Enlarge the dynamically allocated area of the
51f0aa
+   array to make room for one more element.  SCRATCH is a pointer to
51f0aa
+   the scratch area (which is not heap-allocated and must not be
51f0aa
+   freed).  ELEMENT_SIZE is the size, in bytes, of one element.
51f0aa
+   Return false on failure, true on success.  */
51f0aa
+bool __libc_dynarray_emplace_enlarge (struct dynarray_header *,
51f0aa
+                                      void *scratch, size_t element_size);
51f0aa
+
51f0aa
+/* Internal function.  Enlarge the dynamically allocated area of the
51f0aa
+   array to make room for at least SIZE elements (which must be larger
51f0aa
+   than the existing used part of the dynamic array).  SCRATCH is a
51f0aa
+   pointer to the scratch area (which is not heap-allocated and must
51f0aa
+   not be freed).  ELEMENT_SIZE is the size, in bytes, of one element.
51f0aa
+   Return false on failure, true on success.  */
51f0aa
+bool __libc_dynarray_resize (struct dynarray_header *, size_t size,
51f0aa
+                             void *scratch, size_t element_size);
51f0aa
+
51f0aa
+/* Internal function.  Like __libc_dynarray_resize, but clear the new
51f0aa
+   part of the dynamic array.  */
51f0aa
+bool __libc_dynarray_resize_clear (struct dynarray_header *, size_t size,
51f0aa
+                                   void *scratch, size_t element_size);
51f0aa
+
51f0aa
+/* Internal type.  */
51f0aa
+struct dynarray_finalize_result
51f0aa
+{
51f0aa
+  void *array;
51f0aa
+  size_t length;
51f0aa
+};
51f0aa
+
51f0aa
+/* Internal function.  Copy the dynamically-allocated area to an
51f0aa
+   explicitly-sized heap allocation.  SCRATCH is a pointer to the
51f0aa
+   embedded scratch space.  ELEMENT_SIZE is the size, in bytes, of the
51f0aa
+   element type.  On success, true is returned, and pointer and length
51f0aa
+   are written to *RESULT.  On failure, false is returned.  The caller
51f0aa
+   has to take care of some of the memory management; this function is
51f0aa
+   expected to be called from dynarray-skeleton.c.  */
51f0aa
+bool __libc_dynarray_finalize (struct dynarray_header *list, void *scratch,
51f0aa
+                               size_t element_size,
51f0aa
+                               struct dynarray_finalize_result *result);
51f0aa
+
51f0aa
+
51f0aa
+/* Internal function.  Terminate the process after an index error.
51f0aa
+   SIZE is the number of elements of the dynamic array.  INDEX is the
51f0aa
+   lookup index which triggered the failure.  */
51f0aa
+void __libc_dynarray_at_failure (size_t size, size_t index)
51f0aa
+  __attribute__ ((noreturn));
51f0aa
+
51f0aa
+#ifndef _ISOMAC
51f0aa
+libc_hidden_proto (__libc_dynarray_emplace_enlarge)
51f0aa
+libc_hidden_proto (__libc_dynarray_resize)
51f0aa
+libc_hidden_proto (__libc_dynarray_resize_clear)
51f0aa
+libc_hidden_proto (__libc_dynarray_finalize)
51f0aa
+libc_hidden_proto (__libc_dynarray_at_failure)
51f0aa
+#endif
51f0aa
+
51f0aa
+#endif /* _DYNARRAY_H */
51f0aa
diff --git a/malloc/dynarray_at_failure.c b/malloc/dynarray_at_failure.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..fcc06f030b035165
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray_at_failure.c
51f0aa
@@ -0,0 +1,31 @@
51f0aa
+/* Report an dynamic array index out of bounds condition.
51f0aa
+   Copyright (C) 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 <dynarray.h>
51f0aa
+#include <stdio.h>
51f0aa
+
51f0aa
+void
51f0aa
+__libc_dynarray_at_failure (size_t size, size_t index)
51f0aa
+{
51f0aa
+  char buf[200];
51f0aa
+  __snprintf (buf, sizeof (buf), "Fatal glibc error: "
51f0aa
+              "array index %zu not less than array length %zu\n",
51f0aa
+              index, size);
51f0aa
+ __libc_fatal (buf);
51f0aa
+}
51f0aa
+libc_hidden_def (__libc_dynarray_at_failure)
51f0aa
diff --git a/malloc/dynarray_emplace_enlarge.c b/malloc/dynarray_emplace_enlarge.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..a15245f4cb3d4288
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray_emplace_enlarge.c
51f0aa
@@ -0,0 +1,73 @@
51f0aa
+/* Increase the size of a dynamic array in preparation of an emplace operation.
51f0aa
+   Copyright (C) 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 <dynarray.h>
51f0aa
+#include <errno.h>
51f0aa
+#include <malloc-internal.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+bool
51f0aa
+__libc_dynarray_emplace_enlarge (struct dynarray_header *list,
51f0aa
+                                 void *scratch, size_t element_size)
51f0aa
+{
51f0aa
+  size_t new_allocated;
51f0aa
+  if (list->allocated == 0)
51f0aa
+    {
51f0aa
+      /* No scratch buffer provided.  Choose a reasonable default
51f0aa
+         size.  */
51f0aa
+      if (element_size < 4)
51f0aa
+        new_allocated = 16;
51f0aa
+      else if (element_size < 8)
51f0aa
+        new_allocated = 8;
51f0aa
+      else
51f0aa
+        new_allocated = 4;
51f0aa
+    }
51f0aa
+  else
51f0aa
+    /* Increase the allocated size, using an exponential growth
51f0aa
+       policy.  */
51f0aa
+    {
51f0aa
+      new_allocated = list->allocated + list->allocated / 2 + 1;
51f0aa
+      if (new_allocated <= list->allocated)
51f0aa
+        {
51f0aa
+          /* Overflow.  */
51f0aa
+          __set_errno (ENOMEM);
51f0aa
+          return false;
51f0aa
+        }
51f0aa
+    }
51f0aa
+
51f0aa
+  size_t new_size;
51f0aa
+  if (check_mul_overflow_size_t (new_allocated, element_size, &new_size))
51f0aa
+    return false;
51f0aa
+  void *new_array;
51f0aa
+  if (list->array == scratch)
51f0aa
+    {
51f0aa
+      /* The previous array was not heap-allocated.  */
51f0aa
+      new_array = malloc (new_size);
51f0aa
+      if (new_array != NULL && list->array != NULL)
51f0aa
+        memcpy (new_array, list->array, list->used * element_size);
51f0aa
+    }
51f0aa
+  else
51f0aa
+    new_array = realloc (list->array, new_size);
51f0aa
+  if (new_array == NULL)
51f0aa
+    return false;
51f0aa
+  list->array = new_array;
51f0aa
+  list->allocated = new_allocated;
51f0aa
+  return true;
51f0aa
+}
51f0aa
+libc_hidden_def (__libc_dynarray_emplace_enlarge)
51f0aa
diff --git a/malloc/dynarray_finalize.c b/malloc/dynarray_finalize.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..6dd8705382c73ae8
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray_finalize.c
51f0aa
@@ -0,0 +1,62 @@
51f0aa
+/* Copy the dynamically-allocated area to an explicitly-sized heap allocation.
51f0aa
+   Copyright (C) 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 <dynarray.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+bool
51f0aa
+__libc_dynarray_finalize (struct dynarray_header *list,
51f0aa
+                          void *scratch, size_t element_size,
51f0aa
+                          struct dynarray_finalize_result *result)
51f0aa
+{
51f0aa
+  if (__dynarray_error (list))
51f0aa
+    /* The caller will reported the deferred error.  */
51f0aa
+    return false;
51f0aa
+
51f0aa
+  size_t used = list->used;
51f0aa
+
51f0aa
+  /* Empty list.  */
51f0aa
+  if (used == 0)
51f0aa
+    {
51f0aa
+      /* An empty list could still be backed by a heap-allocated
51f0aa
+         array.  Free it if necessary.  */
51f0aa
+      if (list->array != scratch)
51f0aa
+        free (list->array);
51f0aa
+      *result = (struct dynarray_finalize_result) { NULL, 0 };
51f0aa
+      return true;
51f0aa
+    }
51f0aa
+
51f0aa
+  size_t allocation_size = used * element_size;
51f0aa
+  void *heap_array = malloc (allocation_size);
51f0aa
+  if (heap_array != NULL)
51f0aa
+    {
51f0aa
+      /* The new array takes ownership of the strings.  */
51f0aa
+      if (list->array != NULL)
51f0aa
+        memcpy (heap_array, list->array, allocation_size);
51f0aa
+      if (list->array != scratch)
51f0aa
+        free (list->array);
51f0aa
+      *result = (struct dynarray_finalize_result)
51f0aa
+        { .array = heap_array, .length = used };
51f0aa
+      return true;
51f0aa
+    }
51f0aa
+  else
51f0aa
+    /* The caller will perform the freeing operation.  */
51f0aa
+    return false;
51f0aa
+}
51f0aa
+libc_hidden_def (__libc_dynarray_finalize)
51f0aa
diff --git a/malloc/dynarray_resize.c b/malloc/dynarray_resize.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..63c981bf61f67145
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray_resize.c
51f0aa
@@ -0,0 +1,64 @@
51f0aa
+/* Increase the size of a dynamic array.
51f0aa
+   Copyright (C) 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 <dynarray.h>
51f0aa
+#include <errno.h>
51f0aa
+#include <malloc-internal.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+bool
51f0aa
+__libc_dynarray_resize (struct dynarray_header *list, size_t size,
51f0aa
+                        void *scratch, size_t element_size)
51f0aa
+{
51f0aa
+  /* The existing allocation provides sufficient room.  */
51f0aa
+  if (size <= list->allocated)
51f0aa
+    {
51f0aa
+      list->used = size;
51f0aa
+      return true;
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Otherwise, use size as the new allocation size.  The caller is
51f0aa
+     expected to provide the final size of the array, so there is no
51f0aa
+     over-allocation here.  */
51f0aa
+
51f0aa
+  size_t new_size_bytes;
51f0aa
+  if (check_mul_overflow_size_t (size, element_size, &new_size_bytes))
51f0aa
+    {
51f0aa
+      /* Overflow.  */
51f0aa
+      __set_errno (ENOMEM);
51f0aa
+      return false;
51f0aa
+    }
51f0aa
+  void *new_array;
51f0aa
+  if (list->array == scratch)
51f0aa
+    {
51f0aa
+      /* The previous array was not heap-allocated.  */
51f0aa
+      new_array = malloc (new_size_bytes);
51f0aa
+      if (new_array != NULL && list->array != NULL)
51f0aa
+        memcpy (new_array, list->array, list->used * element_size);
51f0aa
+    }
51f0aa
+  else
51f0aa
+    new_array = realloc (list->array, new_size_bytes);
51f0aa
+  if (new_array == NULL)
51f0aa
+    return false;
51f0aa
+  list->array = new_array;
51f0aa
+  list->allocated = size;
51f0aa
+  list->used = size;
51f0aa
+  return true;
51f0aa
+}
51f0aa
+libc_hidden_def (__libc_dynarray_resize)
51f0aa
diff --git a/malloc/dynarray_resize_clear.c b/malloc/dynarray_resize_clear.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..0c4ced1d38b77918
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/dynarray_resize_clear.c
51f0aa
@@ -0,0 +1,35 @@
51f0aa
+/* Increase the size of a dynamic array and clear the new part.
51f0aa
+   Copyright (C) 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 <dynarray.h>
51f0aa
+#include <stdlib.h>
51f0aa
+#include <string.h>
51f0aa
+
51f0aa
+bool
51f0aa
+__libc_dynarray_resize_clear (struct dynarray_header *list, size_t size,
51f0aa
+                              void *scratch, size_t element_size)
51f0aa
+{
51f0aa
+  size_t old_size = list->used;
51f0aa
+  if (!__libc_dynarray_resize (list, size, scratch, element_size))
51f0aa
+    return false;
51f0aa
+  /* __libc_dynarray_resize already checked for overflow.  */
51f0aa
+  memset (list->array + (old_size * element_size), 0,
51f0aa
+          (size - old_size) * element_size);
51f0aa
+  return true;
51f0aa
+}
51f0aa
+libc_hidden_def (__libc_dynarray_resize_clear)
51f0aa
diff --git a/malloc/tst-dynarray-at-fail.c b/malloc/tst-dynarray-at-fail.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..8ba5f92b58141c52
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/tst-dynarray-at-fail.c
51f0aa
@@ -0,0 +1,125 @@
51f0aa
+/* Test reporting of out-of-bounds access for dynamic arrays.
51f0aa
+   Copyright (C) 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 "tst-dynarray-shared.h"
51f0aa
+
51f0aa
+#include <signal.h>
51f0aa
+#include <stdint.h>
51f0aa
+#include <string.h>
51f0aa
+#include <support/capture_subprocess.h>
51f0aa
+#include <support/check.h>
51f0aa
+
51f0aa
+/* Run CALLBACK and check that the data on standard error equals
51f0aa
+   EXPECTED.  */
51f0aa
+static void
51f0aa
+check (const char *test, void (*callback) (void *), size_t index,
51f0aa
+       const char *expected)
51f0aa
+{
51f0aa
+  struct support_capture_subprocess result
51f0aa
+    = support_capture_subprocess (callback, &index);
51f0aa
+  if (strcmp (result.err.buffer, expected) != 0)
51f0aa
+    {
51f0aa
+      support_record_failure ();
51f0aa
+      printf ("error: test %s (%zu) unexpected standard error data\n"
51f0aa
+              "  expected: %s\n"
51f0aa
+              "  actual:   %s\n",
51f0aa
+              test, index, expected, result.err.buffer);
51f0aa
+    }
51f0aa
+  TEST_VERIFY (strlen (result.out.buffer) == 0);
51f0aa
+  TEST_VERIFY (WIFSIGNALED (result.status));
51f0aa
+  if (WIFSIGNALED (result.status))
51f0aa
+    TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
51f0aa
+  support_capture_subprocess_free (&result);
51f0aa
+}
51f0aa
+
51f0aa
+/* Try indexing an empty array.  */
51f0aa
+static void
51f0aa
+test_empty (void *closure)
51f0aa
+{
51f0aa
+  size_t *pindex = closure;
51f0aa
+  struct dynarray_int dyn;
51f0aa
+  dynarray_int_init (&dyn);
51f0aa
+  dynarray_int_at (&dyn, *pindex);
51f0aa
+}
51f0aa
+
51f0aa
+/* Try indexing a one-element array.  */
51f0aa
+static void
51f0aa
+test_one (void *closure)
51f0aa
+{
51f0aa
+  size_t *pindex = closure;
51f0aa
+  struct dynarray_int dyn;
51f0aa
+  dynarray_int_init (&dyn);
51f0aa
+  TEST_VERIFY (dynarray_int_resize (&dyn, 1));
51f0aa
+  dynarray_int_at (&dyn, *pindex);
51f0aa
+}
51f0aa
+
51f0aa
+/* Try indexing a longer array.  */
51f0aa
+static void
51f0aa
+test_many (void *closure)
51f0aa
+{
51f0aa
+  size_t *pindex = closure;
51f0aa
+  struct dynarray_int dyn;
51f0aa
+  dynarray_int_init (&dyn);
51f0aa
+  TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
51f0aa
+  dynarray_int_at (&dyn, *pindex);
51f0aa
+}
51f0aa
+
51f0aa
+/* (size_t) -1 for use in string literals.  */
51f0aa
+#if __WORDSIZE == 32
51f0aa
+# define MINUS_1 "4294967295"
51f0aa
+#elif __WORDSIZE == 64
51f0aa
+# define MINUS_1 "18446744073709551615"
51f0aa
+#else
51f0aa
+# error "unknown value for __WORDSIZE"
51f0aa
+#endif
51f0aa
+
51f0aa
+static int
51f0aa
+do_test (void)
51f0aa
+{
51f0aa
+  TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
51f0aa
+
51f0aa
+  check ("test_empty", test_empty, 0,
51f0aa
+         "Fatal glibc error: array index 0 not less than array length 0\n");
51f0aa
+  check ("test_empty", test_empty, 1,
51f0aa
+         "Fatal glibc error: array index 1 not less than array length 0\n");
51f0aa
+  check ("test_empty", test_empty, -1,
51f0aa
+         "Fatal glibc error: array index " MINUS_1
51f0aa
+         " not less than array length 0\n");
51f0aa
+
51f0aa
+  check ("test_one", test_one, 1,
51f0aa
+         "Fatal glibc error: array index 1 not less than array length 1\n");
51f0aa
+  check ("test_one", test_one, 2,
51f0aa
+         "Fatal glibc error: array index 2 not less than array length 1\n");
51f0aa
+  check ("test_one", test_one, -1,
51f0aa
+         "Fatal glibc error: array index " MINUS_1
51f0aa
+         " not less than array length 1\n");
51f0aa
+
51f0aa
+  check ("test_many", test_many, 5371,
51f0aa
+         "Fatal glibc error: array index 5371"
51f0aa
+         " not less than array length 5371\n");
51f0aa
+  check ("test_many", test_many, 5372,
51f0aa
+         "Fatal glibc error: array index 5372"
51f0aa
+         " not less than array length 5371\n");
51f0aa
+  check ("test_many", test_many, -1,
51f0aa
+         "Fatal glibc error: array index " MINUS_1
51f0aa
+         " not less than array length 5371\n");
51f0aa
+
51f0aa
+  return 0;
51f0aa
+}
51f0aa
+
51f0aa
+#include <support/test-driver.c>
51f0aa
diff --git a/malloc/tst-dynarray-fail.c b/malloc/tst-dynarray-fail.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..508dbae93e6bce31
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/tst-dynarray-fail.c
51f0aa
@@ -0,0 +1,418 @@
51f0aa
+/* Test allocation failures with dynamic arrays.
51f0aa
+   Copyright (C) 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
+/* This test is separate from tst-dynarray because it cannot run under
51f0aa
+   valgrind.  */
51f0aa
+
51f0aa
+#include "tst-dynarray-shared.h"
51f0aa
+
51f0aa
+#include <mcheck.h>
51f0aa
+#include <stdio.h>
51f0aa
+#include <support/check.h>
51f0aa
+#include <support/support.h>
51f0aa
+#include <support/xunistd.h>
51f0aa
+#include <sys/mman.h>
51f0aa
+#include <sys/resource.h>
51f0aa
+#include <unistd.h>
51f0aa
+
51f0aa
+/* Data structure to fill up the heap.  */
51f0aa
+struct heap_filler
51f0aa
+{
51f0aa
+  struct heap_filler *next;
51f0aa
+};
51f0aa
+
51f0aa
+/* Allocate objects until the heap is full.  */
51f0aa
+static struct heap_filler *
51f0aa
+fill_heap (void)
51f0aa
+{
51f0aa
+  size_t pad = 4096;
51f0aa
+  struct heap_filler *head = NULL;
51f0aa
+  while (true)
51f0aa
+    {
51f0aa
+      struct heap_filler *new_head = malloc (sizeof (*new_head) + pad);
51f0aa
+      if (new_head == NULL)
51f0aa
+        {
51f0aa
+          if (pad > 0)
51f0aa
+            {
51f0aa
+              /* Try again with smaller allocations.  */
51f0aa
+              pad = 0;
51f0aa
+              continue;
51f0aa
+            }
51f0aa
+          else
51f0aa
+            break;
51f0aa
+        }
51f0aa
+      new_head->next = head;
51f0aa
+      head = new_head;
51f0aa
+    }
51f0aa
+  return head;
51f0aa
+}
51f0aa
+
51f0aa
+/* Free the heap-filling allocations, so that we can continue testing
51f0aa
+   and detect memory leaks elsewhere.  */
51f0aa
+static void
51f0aa
+free_fill_heap (struct heap_filler *head)
51f0aa
+{
51f0aa
+  while (head != NULL)
51f0aa
+    {
51f0aa
+      struct heap_filler *next = head->next;
51f0aa
+      free (head);
51f0aa
+      head = next;
51f0aa
+    }
51f0aa
+}
51f0aa
+
51f0aa
+/* Check allocation failures for int arrays (without an element free
51f0aa
+   function).  */
51f0aa
+static void
51f0aa
+test_int_fail (void)
51f0aa
+{
51f0aa
+  /* Exercise failure in add/emplace.
51f0aa
+
51f0aa
+     do_add: Use emplace (false) or add (true) to add elements.
51f0aa
+     do_finalize: Perform finalization at the end (instead of free).  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
51f0aa
+      {
51f0aa
+        struct dynarray_int dyn;
51f0aa
+        dynarray_int_init (&dyn);
51f0aa
+        size_t count = 0;
51f0aa
+        while (true)
51f0aa
+          {
51f0aa
+            if (do_add)
51f0aa
+              {
51f0aa
+                dynarray_int_add (&dyn, 0);
51f0aa
+                if (dynarray_int_has_failed (&dyn))
51f0aa
+                  break;
51f0aa
+              }
51f0aa
+            else
51f0aa
+              {
51f0aa
+                int *place = dynarray_int_emplace (&dyn);
51f0aa
+                if (place == NULL)
51f0aa
+                  break;
51f0aa
+                TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
51f0aa
+                *place = 0;
51f0aa
+              }
51f0aa
+            ++count;
51f0aa
+          }
51f0aa
+        printf ("info: %s: failure after %zu elements\n", __func__, count);
51f0aa
+        TEST_VERIFY_EXIT (dynarray_int_has_failed (&dyn));
51f0aa
+        if (do_finalize)
51f0aa
+          {
51f0aa
+            struct int_array result = { (int *) (uintptr_t) -1, -1 };
51f0aa
+            TEST_VERIFY_EXIT (!dynarray_int_finalize (&dyn, &result));
51f0aa
+            TEST_VERIFY_EXIT (result.array == (int *) (uintptr_t) -1);
51f0aa
+            TEST_VERIFY_EXIT (result.length == (size_t) -1);
51f0aa
+          }
51f0aa
+        else
51f0aa
+          dynarray_int_free (&dyn);
51f0aa
+        CHECK_INIT_STATE (int, &dyn);
51f0aa
+      }
51f0aa
+
51f0aa
+  /* Exercise failure in finalize.  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    {
51f0aa
+      struct dynarray_int dyn;
51f0aa
+      dynarray_int_init (&dyn);
51f0aa
+      for (unsigned int i = 0; i < 10000; ++i)
51f0aa
+        {
51f0aa
+          if (do_add)
51f0aa
+            {
51f0aa
+              dynarray_int_add (&dyn, i);
51f0aa
+              TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
51f0aa
+            }
51f0aa
+          else
51f0aa
+            {
51f0aa
+              int *place = dynarray_int_emplace (&dyn);
51f0aa
+              TEST_VERIFY_EXIT (place != NULL);
51f0aa
+              *place = i;
51f0aa
+            }
51f0aa
+        }
51f0aa
+      TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
51f0aa
+      struct heap_filler *heap_filler = fill_heap ();
51f0aa
+      struct int_array result = { (int *) (uintptr_t) -1, -1 };
51f0aa
+      TEST_VERIFY_EXIT (!dynarray_int_finalize (&dyn, &result));
51f0aa
+      TEST_VERIFY_EXIT (result.array == (int *) (uintptr_t) -1);
51f0aa
+      TEST_VERIFY_EXIT (result.length == (size_t) -1);
51f0aa
+      CHECK_INIT_STATE (int, &dyn);
51f0aa
+      free_fill_heap (heap_filler);
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Exercise failure in resize.  */
51f0aa
+  {
51f0aa
+    struct dynarray_int dyn;
51f0aa
+    dynarray_int_init (&dyn);
51f0aa
+    struct heap_filler *heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_int_resize (&dyn, 1000));
51f0aa
+    TEST_VERIFY (dynarray_int_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+
51f0aa
+    dynarray_int_init (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_int_resize (&dyn, 1));
51f0aa
+    heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_int_resize (&dyn, 1000));
51f0aa
+    TEST_VERIFY (dynarray_int_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+
51f0aa
+    dynarray_int_init (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_int_resize (&dyn, 1000));
51f0aa
+    heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_int_resize (&dyn, 2000));
51f0aa
+    TEST_VERIFY (dynarray_int_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+/* Check allocation failures for char * arrays (which automatically
51f0aa
+   free the pointed-to strings).  */
51f0aa
+static void
51f0aa
+test_str_fail (void)
51f0aa
+{
51f0aa
+  /* Exercise failure in add/emplace.
51f0aa
+
51f0aa
+     do_add: Use emplace (false) or add (true) to add elements.
51f0aa
+     do_finalize: Perform finalization at the end (instead of free).  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
51f0aa
+      {
51f0aa
+        struct dynarray_str dyn;
51f0aa
+        dynarray_str_init (&dyn);
51f0aa
+        size_t count = 0;
51f0aa
+        while (true)
51f0aa
+          {
51f0aa
+            char **place;
51f0aa
+            if (do_add)
51f0aa
+              {
51f0aa
+                dynarray_str_add (&dyn, NULL);
51f0aa
+                if (dynarray_str_has_failed (&dyn))
51f0aa
+                  break;
51f0aa
+                else
51f0aa
+                  place = dynarray_str_at (&dyn, dynarray_str_size (&dyn) - 1);
51f0aa
+              }
51f0aa
+            else
51f0aa
+              {
51f0aa
+                place = dynarray_str_emplace (&dyn);
51f0aa
+                if (place == NULL)
51f0aa
+                  break;
51f0aa
+              }
51f0aa
+            TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+            TEST_VERIFY_EXIT (*place == NULL);
51f0aa
+            *place = strdup ("placeholder");
51f0aa
+            if (*place == NULL)
51f0aa
+              {
51f0aa
+                /* Second loop to wait for failure of
51f0aa
+                   dynarray_str_emplace.  */
51f0aa
+                while (true)
51f0aa
+                  {
51f0aa
+                    if (do_add)
51f0aa
+                      {
51f0aa
+                        dynarray_str_add (&dyn, NULL);
51f0aa
+                        if (dynarray_str_has_failed (&dyn))
51f0aa
+                          break;
51f0aa
+                      }
51f0aa
+                    else
51f0aa
+                      {
51f0aa
+                        char **place = dynarray_str_emplace (&dyn);
51f0aa
+                        if (place == NULL)
51f0aa
+                          break;
51f0aa
+                        TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+                        *place = NULL;
51f0aa
+                      }
51f0aa
+                    ++count;
51f0aa
+                  }
51f0aa
+                break;
51f0aa
+              }
51f0aa
+            ++count;
51f0aa
+          }
51f0aa
+        printf ("info: %s: failure after %zu elements\n", __func__, count);
51f0aa
+        TEST_VERIFY_EXIT (dynarray_str_has_failed (&dyn));
51f0aa
+        if (do_finalize)
51f0aa
+          {
51f0aa
+            struct str_array result = { (char **) (uintptr_t) -1, -1 };
51f0aa
+            TEST_VERIFY_EXIT (!dynarray_str_finalize (&dyn, &result));
51f0aa
+            TEST_VERIFY_EXIT (result.array == (char **) (uintptr_t) -1);
51f0aa
+            TEST_VERIFY_EXIT (result.length == (size_t) -1);
51f0aa
+          }
51f0aa
+        else
51f0aa
+          dynarray_str_free (&dyn);
51f0aa
+        TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+        TEST_VERIFY_EXIT (dyn.dynarray_header.array == dyn.scratch);
51f0aa
+        TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == 0);
51f0aa
+        TEST_VERIFY_EXIT (dyn.dynarray_header.allocated > 0);
51f0aa
+      }
51f0aa
+
51f0aa
+  /* Exercise failure in finalize.  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    {
51f0aa
+      struct dynarray_str dyn;
51f0aa
+      dynarray_str_init (&dyn);
51f0aa
+      for (unsigned int i = 0; i < 1000; ++i)
51f0aa
+        {
51f0aa
+          if (do_add)
51f0aa
+            dynarray_str_add (&dyn, xstrdup ("placeholder"));
51f0aa
+          else
51f0aa
+            {
51f0aa
+              char **place = dynarray_str_emplace (&dyn);
51f0aa
+              TEST_VERIFY_EXIT (place != NULL);
51f0aa
+              TEST_VERIFY_EXIT (*place == NULL);
51f0aa
+              *place = xstrdup ("placeholder");
51f0aa
+            }
51f0aa
+        }
51f0aa
+      TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+      struct heap_filler *heap_filler = fill_heap ();
51f0aa
+      struct str_array result = { (char **) (uintptr_t) -1, -1 };
51f0aa
+      TEST_VERIFY_EXIT (!dynarray_str_finalize (&dyn, &result));
51f0aa
+      TEST_VERIFY_EXIT (result.array == (char **) (uintptr_t) -1);
51f0aa
+      TEST_VERIFY_EXIT (result.length == (size_t) -1);
51f0aa
+      TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+      TEST_VERIFY_EXIT (dyn.dynarray_header.array == dyn.scratch);
51f0aa
+      TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == 0);
51f0aa
+      TEST_VERIFY_EXIT (dyn.dynarray_header.allocated > 0);
51f0aa
+      free_fill_heap (heap_filler);
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Exercise failure in resize.  */
51f0aa
+  {
51f0aa
+    struct dynarray_str dyn;
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+    struct heap_filler *heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_str_resize (&dyn, 1000));
51f0aa
+    TEST_VERIFY (dynarray_str_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 1));
51f0aa
+    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated");
51f0aa
+    heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_str_resize (&dyn, 1000));
51f0aa
+    TEST_VERIFY (dynarray_str_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 1000));
51f0aa
+    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated");
51f0aa
+    heap_filler = fill_heap ();
51f0aa
+    TEST_VERIFY (!dynarray_str_resize (&dyn, 2000));
51f0aa
+    TEST_VERIFY (dynarray_str_has_failed (&dyn));
51f0aa
+    free_fill_heap (heap_filler);
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+/* Test if mmap can allocate a page.  This is necessary because
51f0aa
+   setrlimit does not fail even if it reduces the RLIMIT_AS limit
51f0aa
+   below what is currently needed by the process.  */
51f0aa
+static bool
51f0aa
+mmap_works (void)
51f0aa
+{
51f0aa
+  void *ptr =  mmap (NULL, 1, PROT_READ | PROT_WRITE,
51f0aa
+                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
51f0aa
+  if (ptr == MAP_FAILED)
51f0aa
+    return false;
51f0aa
+  xmunmap (ptr, 1);
51f0aa
+  return true;
51f0aa
+}
51f0aa
+
51f0aa
+/* Set the RLIMIT_AS limit to the value in *LIMIT.  */
51f0aa
+static void
51f0aa
+xsetrlimit_as (const struct rlimit *limit)
51f0aa
+{
51f0aa
+  if (setrlimit (RLIMIT_AS, limit) != 0)
51f0aa
+    FAIL_EXIT1 ("setrlimit (RLIMIT_AS, %lu): %m",
51f0aa
+                (unsigned long) limit->rlim_cur);
51f0aa
+}
51f0aa
+
51f0aa
+/* Approximately this many bytes can be allocated after
51f0aa
+   reduce_rlimit_as has run.  */
51f0aa
+enum { as_limit_reserve = 2 * 1024 * 1024 };
51f0aa
+
51f0aa
+/* Limit the size of the process, so that memory allocation in
51f0aa
+   allocate_thread will eventually fail, without impacting the entire
51f0aa
+   system.  By default, a dynamic limit which leaves room for 2 MiB is
51f0aa
+   activated.  The TEST_RLIMIT_AS environment variable overrides
51f0aa
+   it.  */
51f0aa
+static void
51f0aa
+reduce_rlimit_as (void)
51f0aa
+{
51f0aa
+  struct rlimit limit;
51f0aa
+  if (getrlimit (RLIMIT_AS, &limit) != 0)
51f0aa
+    FAIL_EXIT1 ("getrlimit (RLIMIT_AS) failed: %m");
51f0aa
+
51f0aa
+  /* Use the TEST_RLIMIT_AS setting if available.  */
51f0aa
+  {
51f0aa
+    long target = 0;
51f0aa
+    const char *variable = "TEST_RLIMIT_AS";
51f0aa
+    const char *target_str = getenv (variable);
51f0aa
+    if (target_str != NULL)
51f0aa
+      {
51f0aa
+        target = atoi (target_str);
51f0aa
+        if (target <= 0)
51f0aa
+          FAIL_EXIT1 ("invalid %s value: \"%s\"", variable, target_str);
51f0aa
+        printf ("info: setting RLIMIT_AS to %ld MiB\n", target);
51f0aa
+        target *= 1024 * 1024;      /* Convert to megabytes.  */
51f0aa
+        limit.rlim_cur = target;
51f0aa
+        xsetrlimit_as (&limit);
51f0aa
+        return;
51f0aa
+      }
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Otherwise, try to find the limit with a binary search.  */
51f0aa
+  unsigned long low = 1 << 20;
51f0aa
+  limit.rlim_cur = low;
51f0aa
+  xsetrlimit_as (&limit);
51f0aa
+
51f0aa
+  /* Find working upper limit.  */
51f0aa
+  unsigned long high = 1 << 30;
51f0aa
+  while (true)
51f0aa
+    {
51f0aa
+      limit.rlim_cur = high;
51f0aa
+      xsetrlimit_as (&limit);
51f0aa
+      if (mmap_works ())
51f0aa
+        break;
51f0aa
+      if (2 * high < high)
51f0aa
+        FAIL_EXIT1 ("cannot find upper AS limit");
51f0aa
+      high *= 2;
51f0aa
+    }
51f0aa
+
51f0aa
+  /* Perform binary search.  */
51f0aa
+  while ((high - low) > 128 * 1024)
51f0aa
+    {
51f0aa
+      unsigned long middle = (low + high) / 2;
51f0aa
+      limit.rlim_cur = middle;
51f0aa
+      xsetrlimit_as (&limit);
51f0aa
+      if (mmap_works ())
51f0aa
+        high = middle;
51f0aa
+      else
51f0aa
+        low = middle;
51f0aa
+    }
51f0aa
+
51f0aa
+  unsigned long target = high + as_limit_reserve;
51f0aa
+  limit.rlim_cur = target;
51f0aa
+  xsetrlimit_as (&limit);
51f0aa
+  printf ("info: RLIMIT_AS limit: %lu bytes\n", target);
51f0aa
+}
51f0aa
+
51f0aa
+static int
51f0aa
+do_test (void)
51f0aa
+{
51f0aa
+  mtrace ();
51f0aa
+  reduce_rlimit_as ();
51f0aa
+  test_int_fail ();
51f0aa
+  test_str_fail ();
51f0aa
+  return 0;
51f0aa
+}
51f0aa
+
51f0aa
+#define TIMEOUT 90
51f0aa
+#include <support/test-driver.c>
51f0aa
diff --git a/malloc/tst-dynarray-shared.h b/malloc/tst-dynarray-shared.h
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..1de9c04be88843d0
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/tst-dynarray-shared.h
51f0aa
@@ -0,0 +1,80 @@
51f0aa
+/* Shared definitions for dynarray tests.
51f0aa
+   Copyright (C) 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 <stddef.h>
51f0aa
+
51f0aa
+struct int_array
51f0aa
+{
51f0aa
+  int *array;
51f0aa
+  size_t length;
51f0aa
+};
51f0aa
+
51f0aa
+#define DYNARRAY_STRUCT dynarray_int
51f0aa
+#define DYNARRAY_ELEMENT int
51f0aa
+#define DYNARRAY_PREFIX dynarray_int_
51f0aa
+#define DYNARRAY_FINAL_TYPE struct int_array
51f0aa
+#include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+struct str_array
51f0aa
+{
51f0aa
+  char **array;
51f0aa
+  size_t length;
51f0aa
+};
51f0aa
+
51f0aa
+#define DYNARRAY_STRUCT dynarray_str
51f0aa
+#define DYNARRAY_ELEMENT char *
51f0aa
+#define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr)
51f0aa
+#define DYNARRAY_PREFIX dynarray_str_
51f0aa
+#define DYNARRAY_FINAL_TYPE struct str_array
51f0aa
+#include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+/* Check that *DYN is equivalent to its initial state.  */
51f0aa
+#define CHECK_INIT_STATE(type, dyn)                             \
51f0aa
+  ({                                                            \
51f0aa
+    TEST_VERIFY_EXIT (!dynarray_##type##_has_failed (dyn));     \
51f0aa
+    TEST_VERIFY_EXIT (dynarray_##type##_size (dyn) == 0);       \
51f0aa
+    TEST_VERIFY_EXIT ((dyn)->dynarray_header.array              \
51f0aa
+                      == (dyn)->scratch);                       \
51f0aa
+    TEST_VERIFY_EXIT ((dyn)->dynarray_header.allocated > 0);    \
51f0aa
+    (void) 0;                                                   \
51f0aa
+  })
51f0aa
+
51f0aa
+/* Check that *DYN behaves as if it is in its initial state.  */
51f0aa
+#define CHECK_EMPTY(type, dyn)                                       \
51f0aa
+  ({                                                                 \
51f0aa
+    CHECK_INIT_STATE (type, (dyn));                                  \
51f0aa
+    dynarray_##type##_free (dyn);                                    \
51f0aa
+    CHECK_INIT_STATE (type, (dyn));                                  \
51f0aa
+    dynarray_##type##_clear (dyn);                                   \
51f0aa
+    CHECK_INIT_STATE (type, (dyn));                                  \
51f0aa
+    dynarray_##type##_remove_last (dyn);                             \
51f0aa
+    CHECK_INIT_STATE (type, (dyn));                                  \
51f0aa
+    dynarray_##type##_mark_failed (dyn);                             \
51f0aa
+    TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
51f0aa
+    dynarray_##type##_clear (dyn);                                   \
51f0aa
+    TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
51f0aa
+    dynarray_##type##_remove_last (dyn);                             \
51f0aa
+    TEST_VERIFY_EXIT (dynarray_##type##_has_failed (dyn));           \
51f0aa
+    TEST_VERIFY_EXIT (dynarray_##type##_emplace (dyn) == NULL);      \
51f0aa
+    dynarray_##type##_free (dyn);                                    \
51f0aa
+    CHECK_INIT_STATE (type, (dyn));                                  \
51f0aa
+    /* These functions should not assert.  */                        \
51f0aa
+    dynarray_##type##_begin (dyn);                                   \
51f0aa
+    dynarray_##type##_end (dyn);                                     \
51f0aa
+    (void) 0;                                                        \
51f0aa
+  })
51f0aa
diff --git a/malloc/tst-dynarray.c b/malloc/tst-dynarray.c
51f0aa
new file mode 100644
51f0aa
index 0000000000000000..d11f7bb8a343a16a
51f0aa
--- /dev/null
51f0aa
+++ b/malloc/tst-dynarray.c
51f0aa
@@ -0,0 +1,574 @@
51f0aa
+/* Test for dynamic arrays.
51f0aa
+   Copyright (C) 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 "tst-dynarray-shared.h"
51f0aa
+
51f0aa
+#include <errno.h>
51f0aa
+#include <stdint.h>
51f0aa
+
51f0aa
+#define DYNARRAY_STRUCT dynarray_long
51f0aa
+#define DYNARRAY_ELEMENT long
51f0aa
+#define DYNARRAY_PREFIX dynarray_long_
51f0aa
+#define DYNARRAY_ELEMENT_INIT(e) (*(e) = 17)
51f0aa
+#include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+struct long_array
51f0aa
+{
51f0aa
+  long *array;
51f0aa
+  size_t length;
51f0aa
+};
51f0aa
+
51f0aa
+#define DYNARRAY_STRUCT dynarray_long_noscratch
51f0aa
+#define DYNARRAY_ELEMENT long
51f0aa
+#define DYNARRAY_PREFIX dynarray_long_noscratch_
51f0aa
+#define DYNARRAY_ELEMENT_INIT(e) (*(e) = 23)
51f0aa
+#define DYNARRAY_FINAL_TYPE struct long_array
51f0aa
+#define DYNARRAY_INITIAL_SIZE 0
51f0aa
+#include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+#define DYNARRAY_STRUCT zstr
51f0aa
+#define DYNARRAY_ELEMENT char
51f0aa
+#define DYNARRAY_PREFIX zstr_
51f0aa
+#define DYNARRAY_INITIAL_SIZE 128
51f0aa
+#include <malloc/dynarray-skeleton.c>
51f0aa
+
51f0aa
+#include <malloc.h>
51f0aa
+#include <mcheck.h>
51f0aa
+#include <stdint.h>
51f0aa
+#include <support/check.h>
51f0aa
+#include <support/support.h>
51f0aa
+
51f0aa
+enum { max_count = 20 };
51f0aa
+
51f0aa
+/* Test dynamic arrays with int elements (no automatic deallocation
51f0aa
+   for elements).  */
51f0aa
+static void
51f0aa
+test_int (void)
51f0aa
+{
51f0aa
+  /* Empty array.  */
51f0aa
+  {
51f0aa
+    struct dynarray_int dyn;
51f0aa
+    dynarray_int_init (&dyn);
51f0aa
+    CHECK_EMPTY (int, &dyn);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Empty array with finalization.  */
51f0aa
+  {
51f0aa
+    struct dynarray_int dyn;
51f0aa
+    dynarray_int_init (&dyn);
51f0aa
+    CHECK_INIT_STATE (int, &dyn);
51f0aa
+    struct int_array result = { (int *) (uintptr_t) -1, -1 };
51f0aa
+    TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn, &result));
51f0aa
+    CHECK_INIT_STATE (int, &dyn);
51f0aa
+    TEST_VERIFY_EXIT (result.array == NULL);
51f0aa
+    TEST_VERIFY_EXIT (result.length == 0);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Non-empty array tests.
51f0aa
+
51f0aa
+     do_add: Switch between emplace (false) and add (true).
51f0aa
+     do_finalize: Perform finalize call at the end.
51f0aa
+     do_clear: Perform clear call at the end.
51f0aa
+     do_remove_last: Perform remove_last call after adding elements.
51f0aa
+     count: Number of elements added to the array.  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
51f0aa
+      for (int do_clear = 0; do_clear < 2; ++do_clear)
51f0aa
+        for (int do_remove_last = 0; do_remove_last < 2; ++do_remove_last)
51f0aa
+          for (unsigned int count = 0; count < max_count; ++count)
51f0aa
+            {
51f0aa
+              if (do_remove_last && count == 0)
51f0aa
+                continue;
51f0aa
+              unsigned int base = count * count;
51f0aa
+              struct dynarray_int dyn;
51f0aa
+              dynarray_int_init (&dyn);
51f0aa
+              for (unsigned int i = 0; i < count; ++i)
51f0aa
+                {
51f0aa
+                  if (do_add)
51f0aa
+                    dynarray_int_add (&dyn, base + i);
51f0aa
+                  else
51f0aa
+                    {
51f0aa
+                      int *place = dynarray_int_emplace (&dyn);
51f0aa
+                      TEST_VERIFY_EXIT (place != NULL);
51f0aa
+                      *place = base + i;
51f0aa
+                    }
51f0aa
+                  TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == i + 1);
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_int_size (&dyn)
51f0aa
+                                    <= dyn.dynarray_header.allocated);
51f0aa
+                }
51f0aa
+              TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == count);
51f0aa
+              TEST_VERIFY_EXIT (count <= dyn.dynarray_header.allocated);
51f0aa
+              if (count > 0)
51f0aa
+                {
51f0aa
+                  TEST_VERIFY (dynarray_int_begin (&dyn)
51f0aa
+                               == dynarray_int_at (&dyn, 0));
51f0aa
+                  TEST_VERIFY (dynarray_int_end (&dyn)
51f0aa
+                               == dynarray_int_at (&dyn, count - 1) + 1);
51f0aa
+                }
51f0aa
+              unsigned final_count;
51f0aa
+              bool heap_array = dyn.dynarray_header.array != dyn.scratch;
51f0aa
+              if (do_remove_last)
51f0aa
+                {
51f0aa
+                  dynarray_int_remove_last (&dyn);
51f0aa
+                  if (count == 0)
51f0aa
+                    final_count = 0;
51f0aa
+                  else
51f0aa
+                    final_count = count - 1;
51f0aa
+                }
51f0aa
+              else
51f0aa
+                final_count = count;
51f0aa
+              if (final_count > 0)
51f0aa
+                {
51f0aa
+                  TEST_VERIFY (dynarray_int_begin (&dyn)
51f0aa
+                               == dynarray_int_at (&dyn, 0));
51f0aa
+                  TEST_VERIFY (dynarray_int_end (&dyn)
51f0aa
+                               == dynarray_int_at (&dyn, final_count - 1) + 1);
51f0aa
+                }
51f0aa
+              if (do_clear)
51f0aa
+                {
51f0aa
+                  dynarray_int_clear (&dyn);
51f0aa
+                  final_count = 0;
51f0aa
+                }
51f0aa
+              TEST_VERIFY_EXIT (!dynarray_int_has_failed (&dyn));
51f0aa
+              TEST_VERIFY_EXIT ((dyn.dynarray_header.array != dyn.scratch)
51f0aa
+                                == heap_array);
51f0aa
+              TEST_VERIFY_EXIT (dynarray_int_size (&dyn) == final_count);
51f0aa
+              TEST_VERIFY_EXIT (dyn.dynarray_header.allocated >= final_count);
51f0aa
+              if (!do_clear)
51f0aa
+                for (unsigned int i = 0; i < final_count; ++i)
51f0aa
+                  TEST_VERIFY_EXIT (*dynarray_int_at (&dyn, i) == base + i);
51f0aa
+              if (do_finalize)
51f0aa
+                {
51f0aa
+                  struct int_array result = { (int *) (uintptr_t) -1, -1 };
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_int_finalize (&dyn, &result));
51f0aa
+                  CHECK_INIT_STATE (int, &dyn);
51f0aa
+                  TEST_VERIFY_EXIT (result.length == final_count);
51f0aa
+                  if (final_count == 0)
51f0aa
+                    TEST_VERIFY_EXIT (result.array == NULL);
51f0aa
+                  else
51f0aa
+                    {
51f0aa
+                      TEST_VERIFY_EXIT (result.array != NULL);
51f0aa
+                      TEST_VERIFY_EXIT (result.array != (int *) (uintptr_t) -1);
51f0aa
+                      TEST_VERIFY_EXIT
51f0aa
+                        (malloc_usable_size (result.array)
51f0aa
+                         >= final_count * sizeof (result.array[0]));
51f0aa
+                      for (unsigned int i = 0; i < final_count; ++i)
51f0aa
+                        TEST_VERIFY_EXIT (result.array[i] == base + i);
51f0aa
+                      free (result.array);
51f0aa
+                    }
51f0aa
+                }
51f0aa
+              else /* !do_finalize */
51f0aa
+                {
51f0aa
+                  dynarray_int_free (&dyn);
51f0aa
+                  CHECK_INIT_STATE (int, &dyn);
51f0aa
+                }
51f0aa
+            }
51f0aa
+}
51f0aa
+
51f0aa
+/* Test dynamic arrays with char * elements (with automatic
51f0aa
+   deallocation of the pointed-to strings).  */
51f0aa
+static void
51f0aa
+test_str (void)
51f0aa
+{
51f0aa
+  /* Empty array.  */
51f0aa
+  {
51f0aa
+    struct dynarray_str dyn;
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+    CHECK_EMPTY (str, &dyn);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Empty array with finalization.  */
51f0aa
+  {
51f0aa
+    struct dynarray_str dyn;
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+    TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+    struct str_array result = { (char **) (uintptr_t) -1, -1 };
51f0aa
+    TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn, &result));
51f0aa
+    CHECK_INIT_STATE (str, &dyn);
51f0aa
+    TEST_VERIFY_EXIT (result.array == NULL);
51f0aa
+    TEST_VERIFY_EXIT (result.length == 0);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Non-empty array tests.
51f0aa
+
51f0aa
+     do_add: Switch between emplace (false) and add (true).
51f0aa
+     do_finalize: Perform finalize call at the end.
51f0aa
+     do_clear: Perform clear call at the end.
51f0aa
+     do_remove_last: Perform remove_last call after adding elements.
51f0aa
+     count: Number of elements added to the array.  */
51f0aa
+  for (int do_add = 0; do_add < 2; ++do_add)
51f0aa
+    for (int do_finalize = 0; do_finalize < 2; ++do_finalize)
51f0aa
+      for (int do_clear = 0; do_clear < 2; ++do_clear)
51f0aa
+        for (int do_remove_last = 0; do_remove_last < 2; ++do_remove_last)
51f0aa
+          for (unsigned int count = 0; count < max_count; ++count)
51f0aa
+            {
51f0aa
+              if (do_remove_last && count == 0)
51f0aa
+                continue;
51f0aa
+              unsigned int base = count * count;
51f0aa
+              struct dynarray_str dyn;
51f0aa
+              dynarray_str_init (&dyn);
51f0aa
+              for (unsigned int i = 0; i < count; ++i)
51f0aa
+                {
51f0aa
+                  char *item = xasprintf ("%d", base + i);
51f0aa
+                  if (do_add)
51f0aa
+                    dynarray_str_add (&dyn, item);
51f0aa
+                  else
51f0aa
+                    {
51f0aa
+                      char **place = dynarray_str_emplace (&dyn);
51f0aa
+                      TEST_VERIFY_EXIT (place != NULL);
51f0aa
+                      TEST_VERIFY_EXIT (*place == NULL);
51f0aa
+                      *place = item;
51f0aa
+                    }
51f0aa
+                  TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == i + 1);
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_str_size (&dyn)
51f0aa
+                                    <= dyn.dynarray_header.allocated);
51f0aa
+                }
51f0aa
+              TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == count);
51f0aa
+              TEST_VERIFY_EXIT (count <= dyn.dynarray_header.allocated);
51f0aa
+              if (count > 0)
51f0aa
+                {
51f0aa
+                  TEST_VERIFY (dynarray_str_begin (&dyn)
51f0aa
+                               == dynarray_str_at (&dyn, 0));
51f0aa
+                  TEST_VERIFY (dynarray_str_end (&dyn)
51f0aa
+                               == dynarray_str_at (&dyn, count - 1) + 1);
51f0aa
+                }
51f0aa
+              unsigned final_count;
51f0aa
+              bool heap_array = dyn.dynarray_header.array != dyn.scratch;
51f0aa
+              if (do_remove_last)
51f0aa
+                {
51f0aa
+                  dynarray_str_remove_last (&dyn);
51f0aa
+                  if (count == 0)
51f0aa
+                    final_count = 0;
51f0aa
+                  else
51f0aa
+                    final_count = count - 1;
51f0aa
+                }
51f0aa
+              else
51f0aa
+                final_count = count;
51f0aa
+              if (final_count > 0)
51f0aa
+                {
51f0aa
+                  TEST_VERIFY (dynarray_str_begin (&dyn)
51f0aa
+                               == dynarray_str_at (&dyn, 0));
51f0aa
+                  TEST_VERIFY (dynarray_str_end (&dyn)
51f0aa
+                               == dynarray_str_at (&dyn, final_count - 1) + 1);
51f0aa
+                }
51f0aa
+              if (do_clear)
51f0aa
+                {
51f0aa
+                  dynarray_str_clear (&dyn);
51f0aa
+                  final_count = 0;
51f0aa
+                }
51f0aa
+              TEST_VERIFY_EXIT (!dynarray_str_has_failed (&dyn));
51f0aa
+              TEST_VERIFY_EXIT ((dyn.dynarray_header.array != dyn.scratch)
51f0aa
+                                == heap_array);
51f0aa
+              TEST_VERIFY_EXIT (dynarray_str_size (&dyn) == final_count);
51f0aa
+              TEST_VERIFY_EXIT (dyn.dynarray_header.allocated >= final_count);
51f0aa
+              if (!do_clear)
51f0aa
+                for (unsigned int i = 0; i < count - do_remove_last; ++i)
51f0aa
+                  {
51f0aa
+                    char *expected = xasprintf ("%d", base + i);
51f0aa
+                    const char *actual = *dynarray_str_at (&dyn, i);
51f0aa
+                    TEST_VERIFY_EXIT (strcmp (actual, expected) == 0);
51f0aa
+                    free (expected);
51f0aa
+                  }
51f0aa
+              if (do_finalize)
51f0aa
+                {
51f0aa
+                  struct str_array result = { (char **) (uintptr_t) -1, -1 };
51f0aa
+                  TEST_VERIFY_EXIT (dynarray_str_finalize (&dyn, &result));
51f0aa
+                  CHECK_INIT_STATE (str, &dyn);
51f0aa
+                  TEST_VERIFY_EXIT (result.length == final_count);
51f0aa
+                  if (final_count == 0)
51f0aa
+                    TEST_VERIFY_EXIT (result.array == NULL);
51f0aa
+                  else
51f0aa
+                    {
51f0aa
+                      TEST_VERIFY_EXIT (result.array != NULL);
51f0aa
+                      TEST_VERIFY_EXIT (result.array
51f0aa
+                                        != (char **) (uintptr_t) -1);
51f0aa
+                      TEST_VERIFY_EXIT (result.length
51f0aa
+                                        == count - do_remove_last);
51f0aa
+                      TEST_VERIFY_EXIT
51f0aa
+                        (malloc_usable_size (result.array)
51f0aa
+                         >= final_count * sizeof (result.array[0]));
51f0aa
+                      for (unsigned int i = 0; i < count - do_remove_last; ++i)
51f0aa
+                        {
51f0aa
+                          char *expected = xasprintf ("%d", base + i);
51f0aa
+                          char *actual = result.array[i];
51f0aa
+                          TEST_VERIFY_EXIT (strcmp (actual, expected) == 0);
51f0aa
+                          free (expected);
51f0aa
+                          free (actual);
51f0aa
+                        }
51f0aa
+                      free (result.array);
51f0aa
+                    }
51f0aa
+                }
51f0aa
+              else /* !do_finalize */
51f0aa
+                {
51f0aa
+                  dynarray_str_free (&dyn);
51f0aa
+                  CHECK_INIT_STATE (str, &dyn);
51f0aa
+                }
51f0aa
+            }
51f0aa
+
51f0aa
+  /* Test resizing.  */
51f0aa
+  {
51f0aa
+    enum { count = 2131 };
51f0aa
+    struct dynarray_str dyn;
51f0aa
+    dynarray_str_init (&dyn);
51f0aa
+
51f0aa
+    /* From length 0 to length 1.  */
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_str_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_str_at (&dyn, 0) == NULL);
51f0aa
+    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated");
51f0aa
+    dynarray_str_free (&dyn);
51f0aa
+
51f0aa
+    /* From length 0 to length 1 and 2.  */
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_str_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_str_at (&dyn, 0) == NULL);
51f0aa
+    *dynarray_str_at (&dyn, 0) = xstrdup ("allocated0");
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 2));
51f0aa
+    TEST_VERIFY (dynarray_str_size (&dyn) == 2);
51f0aa
+    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
51f0aa
+    TEST_VERIFY (*dynarray_str_at (&dyn, 1) == NULL);
51f0aa
+    *dynarray_str_at (&dyn, 1) = xstrdup ("allocated1");
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, count));
51f0aa
+    TEST_VERIFY (dynarray_str_size (&dyn) == count);
51f0aa
+    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
51f0aa
+    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 1), "allocated1") == 0);
51f0aa
+    for (int i = 2; i < count; ++i)
51f0aa
+      TEST_VERIFY (*dynarray_str_at (&dyn, i) == NULL);
51f0aa
+    *dynarray_str_at (&dyn, count - 1) = xstrdup ("allocated2");
51f0aa
+    TEST_VERIFY (dynarray_str_resize (&dyn, 3));
51f0aa
+    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 0), "allocated0") == 0);
51f0aa
+    TEST_VERIFY (strcmp (*dynarray_str_at (&dyn, 1), "allocated1") == 0);
51f0aa
+    TEST_VERIFY (*dynarray_str_at (&dyn, 2) == NULL);
51f0aa
+    dynarray_str_free (&dyn);
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+/* Verify that DYNARRAY_ELEMENT_INIT has an effect.  */
51f0aa
+static void
51f0aa
+test_long_init (void)
51f0aa
+{
51f0aa
+  enum { count = 2131 };
51f0aa
+  {
51f0aa
+    struct dynarray_long dyn;
51f0aa
+    dynarray_long_init (&dyn);
51f0aa
+    for (int i = 0; i < count; ++i)
51f0aa
+      {
51f0aa
+        long *place = dynarray_long_emplace (&dyn);
51f0aa
+        TEST_VERIFY_EXIT (place != NULL);
51f0aa
+        TEST_VERIFY (*place == 17);
51f0aa
+      }
51f0aa
+    TEST_VERIFY (dynarray_long_size (&dyn) == count);
51f0aa
+    for (int i = 0; i < count; ++i)
51f0aa
+      TEST_VERIFY (*dynarray_long_at (&dyn, i) == 17);
51f0aa
+    dynarray_long_free (&dyn);
51f0aa
+
51f0aa
+    TEST_VERIFY (dynarray_long_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_long_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
51f0aa
+    *dynarray_long_at (&dyn, 0) = 18;
51f0aa
+    dynarray_long_free (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_long_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_long_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
51f0aa
+    TEST_VERIFY (dynarray_long_resize (&dyn, 2));
51f0aa
+    TEST_VERIFY (dynarray_long_size (&dyn) == 2);
51f0aa
+    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 17);
51f0aa
+    TEST_VERIFY (*dynarray_long_at (&dyn, 1) == 17);
51f0aa
+    *dynarray_long_at (&dyn, 0) = 18;
51f0aa
+    TEST_VERIFY (dynarray_long_resize (&dyn, count));
51f0aa
+    TEST_VERIFY (dynarray_long_size (&dyn) == count);
51f0aa
+    TEST_VERIFY (*dynarray_long_at (&dyn, 0) == 18);
51f0aa
+    for (int i = 1; i < count; ++i)
51f0aa
+      TEST_VERIFY (*dynarray_long_at (&dyn, i) == 17);
51f0aa
+    dynarray_long_free (&dyn);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Similar, but without an on-stack scratch region
51f0aa
+     (DYNARRAY_INITIAL_SIZE is 0).  */
51f0aa
+  {
51f0aa
+    struct dynarray_long_noscratch dyn;
51f0aa
+    dynarray_long_noscratch_init (&dyn);
51f0aa
+    struct long_array result;
51f0aa
+    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
51f0aa
+    TEST_VERIFY (result.array == NULL);
51f0aa
+    TEST_VERIFY (result.length == 0);
51f0aa
+
51f0aa
+    /* Test with one element.  */
51f0aa
+    {
51f0aa
+      long *place = dynarray_long_noscratch_emplace (&dyn);
51f0aa
+      TEST_VERIFY_EXIT (place != NULL);
51f0aa
+      TEST_VERIFY (*place == 23);
51f0aa
+    }
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
51f0aa
+    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
51f0aa
+    TEST_VERIFY_EXIT (result.array != NULL);
51f0aa
+    TEST_VERIFY (result.length == 1);
51f0aa
+    TEST_VERIFY (result.array[0] == 23);
51f0aa
+    free (result.array);
51f0aa
+
51f0aa
+    for (int i = 0; i < count; ++i)
51f0aa
+      {
51f0aa
+        long *place = dynarray_long_noscratch_emplace (&dyn);
51f0aa
+        TEST_VERIFY_EXIT (place != NULL);
51f0aa
+        TEST_VERIFY (*place == 23);
51f0aa
+        if (i == 0)
51f0aa
+          *place = 29;
51f0aa
+      }
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == count);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 29);
51f0aa
+    for (int i = 1; i < count; ++i)
51f0aa
+      TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, i) == 23);
51f0aa
+    TEST_VERIFY_EXIT (dynarray_long_noscratch_finalize (&dyn, &result));
51f0aa
+    TEST_VERIFY_EXIT (result.array != NULL);
51f0aa
+    TEST_VERIFY (result.length == count);
51f0aa
+    TEST_VERIFY (result.array[0] == 29);
51f0aa
+    for (int i = 1; i < count; ++i)
51f0aa
+      TEST_VERIFY (result.array[i] == 23);
51f0aa
+    free (result.array);
51f0aa
+
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
51f0aa
+    *dynarray_long_noscratch_at (&dyn, 0) = 24;
51f0aa
+    dynarray_long_noscratch_free (&dyn);
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 1));
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 1);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, 2));
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == 2);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 23);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 1) == 23);
51f0aa
+    *dynarray_long_noscratch_at (&dyn, 0) = 24;
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_resize (&dyn, count));
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_size (&dyn) == count);
51f0aa
+    TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, 0) == 24);
51f0aa
+    for (int i = 1; i < count; ++i)
51f0aa
+      TEST_VERIFY (*dynarray_long_noscratch_at (&dyn, i) == 23);
51f0aa
+    dynarray_long_noscratch_free (&dyn);
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+/* Test overflow in resize.  */
51f0aa
+static void
51f0aa
+test_long_overflow (void)
51f0aa
+{
51f0aa
+  {
51f0aa
+    struct dynarray_long dyn;
51f0aa
+    dynarray_long_init (&dyn);
51f0aa
+    errno = EINVAL;
51f0aa
+    TEST_VERIFY (!dynarray_long_resize
51f0aa
+                 (&dyn, (SIZE_MAX / sizeof (long)) + 1));
51f0aa
+    TEST_VERIFY (errno == ENOMEM);
51f0aa
+    TEST_VERIFY (dynarray_long_has_failed (&dyn));
51f0aa
+  }
51f0aa
+
51f0aa
+  {
51f0aa
+    struct dynarray_long_noscratch dyn;
51f0aa
+    dynarray_long_noscratch_init (&dyn);
51f0aa
+    errno = EINVAL;
51f0aa
+    TEST_VERIFY (!dynarray_long_noscratch_resize
51f0aa
+                 (&dyn, (SIZE_MAX / sizeof (long)) + 1));
51f0aa
+    TEST_VERIFY (errno == ENOMEM);
51f0aa
+    TEST_VERIFY (dynarray_long_noscratch_has_failed (&dyn));
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+/* Test NUL-terminated string construction with the add function and
51f0aa
+   the simple finalize function.  */
51f0aa
+static void
51f0aa
+test_zstr (void)
51f0aa
+{
51f0aa
+  /* Totally empty string (no NUL termination).  */
51f0aa
+  {
51f0aa
+    struct zstr s;
51f0aa
+    zstr_init (&s);
51f0aa
+    char *result = zstr_finalize (&s, NULL);
51f0aa
+    TEST_VERIFY (result == NULL);
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+    size_t length = 1;
51f0aa
+    result = zstr_finalize (&s, &length);
51f0aa
+    TEST_VERIFY (result == NULL);
51f0aa
+    TEST_VERIFY (length == 0);
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* Empty string.  */
51f0aa
+  {
51f0aa
+    struct zstr s;
51f0aa
+    zstr_init (&s);
51f0aa
+    zstr_add (&s, '\0');
51f0aa
+    char *result = zstr_finalize (&s, NULL);
51f0aa
+    TEST_VERIFY_EXIT (result != NULL);
51f0aa
+    TEST_VERIFY (*result == '\0');
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+    free (result);
51f0aa
+
51f0aa
+    zstr_add (&s, '\0');
51f0aa
+    size_t length = 1;
51f0aa
+    result = zstr_finalize (&s, &length);
51f0aa
+    TEST_VERIFY_EXIT (result != NULL);
51f0aa
+    TEST_VERIFY (*result == '\0');
51f0aa
+    TEST_VERIFY (length == 1);
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+    free (result);
51f0aa
+  }
51f0aa
+
51f0aa
+  /* A few characters.  */
51f0aa
+  {
51f0aa
+    struct zstr s;
51f0aa
+    zstr_init (&s);
51f0aa
+    zstr_add (&s, 'A');
51f0aa
+    zstr_add (&s, 'b');
51f0aa
+    zstr_add (&s, 'c');
51f0aa
+    zstr_add (&s, '\0');
51f0aa
+    char *result = zstr_finalize (&s, NULL);
51f0aa
+    TEST_VERIFY_EXIT (result != NULL);
51f0aa
+    TEST_VERIFY (strcmp (result, "Abc") == 0);
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+    free (result);
51f0aa
+
51f0aa
+    zstr_add (&s, 'X');
51f0aa
+    zstr_add (&s, 'y');
51f0aa
+    zstr_add (&s, 'z');
51f0aa
+    zstr_add (&s, '\0');
51f0aa
+    size_t length = 1;
51f0aa
+    result = zstr_finalize (&s, &length);
51f0aa
+    TEST_VERIFY_EXIT (result != NULL);
51f0aa
+    TEST_VERIFY (strcmp (result, "Xyz") == 0);
51f0aa
+    TEST_VERIFY (length == 4);
51f0aa
+    TEST_VERIFY (zstr_size (&s) == 0);
51f0aa
+    free (result);
51f0aa
+  }
51f0aa
+}
51f0aa
+
51f0aa
+static int
51f0aa
+do_test (void)
51f0aa
+{
51f0aa
+  mtrace ();
51f0aa
+  test_int ();
51f0aa
+  test_str ();
51f0aa
+  test_long_init ();
51f0aa
+  test_long_overflow ();
51f0aa
+  test_zstr ();
51f0aa
+  return 0;
51f0aa
+}
51f0aa
+
51f0aa
+#include <support/test-driver.c>