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