INN commit: trunk (18 files)

INN Commit rra at isc.org
Fri Sep 5 19:32:32 UTC 2014


    Date: Friday, September 5, 2014 @ 12:32:31
  Author: iulius
Revision: 9677

sync the xmalloc library with its latest upstream version

Changes are:
- Add xreallocarray to the xmalloc library, which is a checked version of
reallocarray similar to what xrealloc is for realloc.

- Prefer reallocarray to realloc for multiplied sizes in vector
manipulations.

- Also change the memory thresholds so that the xmalloc test works on
amd64 Debian Linux again.

- xasprintf and xvasprintf are now void functions and always call the
xmalloc failure handler on any error, not just on ENOMEM errors.  The
faint chance that the underlying asprintf function could return some
other error isn't worth the additional code complexity of still having
to check the return status and then probably abort anyway.

- In xasprintf and xvasprintf, distinguish between failure to allocate
memory and failure to format the output.  Report the latter by passing
0 to the failure handler, and special-case that in the default failure
handler to report a different error message.

- Avoid excessive memory allocation when duplicating short
nul-terminated strings in xstrndup.

Added:
  trunk/include/inn/xmalloc.h
  trunk/lib/reallocarray.c
  trunk/tests/lib/reallocarray-t.c
Modified:
  trunk/MANIFEST
  trunk/configure.ac
  trunk/include/clibrary.h
  trunk/include/inn/libinn.h
  trunk/lib/buffer.c
  trunk/lib/confparse.c
  trunk/lib/xmalloc.c
  trunk/tests/Makefile
  trunk/tests/TESTS
  trunk/tests/lib/	(properties)
  trunk/tests/lib/getnameinfo-t.c
  trunk/tests/lib/messages-t.c
  trunk/tests/lib/vector-t.c
  trunk/tests/lib/xmalloc.c
  trunk/tests/lib/xmalloc.t

----------------------------+
 MANIFEST                   |    3 
 configure.ac               |    5 
 include/clibrary.h         |    3 
 include/inn/libinn.h       |   65 ------------
 include/inn/xmalloc.h      |  120 ++++++++++++++++++++++
 lib/buffer.c               |    2 
 lib/confparse.c            |   10 -
 lib/reallocarray.c         |   57 ++++++++++
 lib/xmalloc.c              |  231 +++++++++++++++++++++++++++----------------
 tests/Makefile             |    9 +
 tests/TESTS                |    1 
 tests/lib/getnameinfo-t.c  |    2 
 tests/lib/messages-t.c     |    3 
 tests/lib/reallocarray-t.c |   88 ++++++++++++++++
 tests/lib/vector-t.c       |    2 
 tests/lib/xmalloc.c        |  113 ++++++++++++++-------
 tests/lib/xmalloc.t        |  144 ++++++++++++++------------
 17 files changed, 593 insertions(+), 265 deletions(-)

Modified: MANIFEST
===================================================================
--- MANIFEST	2014-09-05 18:47:24 UTC (rev 9676)
+++ MANIFEST	2014-09-05 19:32:31 UTC (rev 9677)
@@ -408,6 +408,7 @@
 include/inn/utility.h                 Header file for utility functions
 include/inn/vector.h                  Header file for vectors of strings
 include/inn/wire.h                    Header file for wire-format functions
+include/inn/xmalloc.h                 Header file for allocation functions
 include/innperl.h                     Header file for embedded Perl
 include/portable                      Portability wrappers (Directory)
 include/portable/alloca.h             alloca replacement header
@@ -527,6 +528,7 @@
 lib/qio.c                             Quick I/O package
 lib/radix32.c                         Encode a number as a radix-32 string
 lib/readin.c                          Read file into memory
+lib/reallocarray.c                    reallocarray replacement
 lib/remopen.c                         Open a remote NNTP connection
 lib/reservedfd.c                      File descriptor reservation
 lib/resource.c                        Get process CPU usage
@@ -889,6 +891,7 @@
 tests/lib/pread-t.c                   Tests for lib/pread.c
 tests/lib/pwrite-t.c                  Tests for lib/pwrite.c
 tests/lib/qio-t.c                     Tests for lib/qio.c
+tests/lib/reallocarray-t.c            Tests for lib/reallocarray.c
 tests/lib/setenv-t.c                  Tests for lib/setenv.c
 tests/lib/snprintf-t.c                Tests for lib/snprintf.c
 tests/lib/strlcat-t.c                 Tests for lib/strlcat.c

Modified: configure.ac
===================================================================
--- configure.ac	2014-09-05 18:47:24 UTC (rev 9676)
+++ configure.ac	2014-09-05 19:32:31 UTC (rev 9677)
@@ -497,8 +497,9 @@
 
 dnl If we can't find any of the following, we have replacements for them.
 AC_REPLACE_FUNCS(asprintf getaddrinfo getnameinfo getpagesize \
-                 inet_aton inet_ntop mkstemp pread pwrite seteuid strcasecmp \
-                 strlcat strlcpy strspn setenv symlink)
+                 inet_aton inet_ntop mkstemp pread pwrite reallocarray \
+                 setenv seteuid strcasecmp \
+                 strlcat strlcpy strspn symlink)
 
 dnl Probe for fseeko and ftello, which take off_t instead of int.
 if test x"$inn_enable_largefiles" = xyes ; then

Modified: include/clibrary.h
===================================================================
--- include/clibrary.h	2014-09-05 18:47:24 UTC (rev 9676)
+++ include/clibrary.h	2014-09-05 19:32:31 UTC (rev 9677)
@@ -106,6 +106,9 @@
 #if !HAVE_DECL_PWRITE
 extern ssize_t          pwrite(int, const void *, size_t, off_t);
 #endif
+#if !HAVE_REALLOCARRAY
+extern void             *reallocarray(void *, size_t, size_t);
+#endif
 #if !HAVE_SETENV
 extern int              setenv(const char *, const char *, int);
 #endif

Modified: include/inn/libinn.h
===================================================================
--- include/inn/libinn.h	2014-09-05 18:47:24 UTC (rev 9676)
+++ include/inn/libinn.h	2014-09-05 19:32:31 UTC (rev 9677)
@@ -7,6 +7,7 @@
 #define LIBINN_H 1
 
 #include <inn/defines.h>
+#include "inn/xmalloc.h"
 
 #include <stdarg.h>             /* va_list */
 #include <stdio.h>              /* FILE */
@@ -29,70 +30,6 @@
 BEGIN_DECLS
 
 /*
-**  MEMORY MANAGEMENT
-*/
-
-/* The functions are actually macros so that we can pick up the file and line
-   number information for debugging error messages without the user having to
-   pass those in every time. */
-#define xcalloc(n, size)        x_calloc((n), (size), __FILE__, __LINE__)
-#define xmalloc(size)           x_malloc((size), __FILE__, __LINE__)
-#define xrealloc(p, size)       x_realloc((p), (size), __FILE__, __LINE__)
-#define xstrdup(p)              x_strdup((p), __FILE__, __LINE__)
-#define xstrndup(p, size)       x_strndup((p), (size), __FILE__, __LINE__)
-#define xvasprintf(p, f, a)     x_vasprintf((p), (f), (a), __FILE__, __LINE__)
-
-/* asprintf is a special case since it takes variable arguments.  If we have
-   support for variadic macros, we can still pass in the file and line and
-   just need to put them somewhere else in the argument list than last.
-   Otherwise, just call x_asprintf directly.  This means that the number of
-   arguments x_asprintf takes must vary depending on whether variadic macros
-   are supported. */
-#ifdef INN_HAVE_C99_VAMACROS
-# define xasprintf(p, f, ...) \
-    x_asprintf((p), __FILE__, __LINE__, (f), __VA_ARGS__)
-#elif INN_HAVE_GNU_VAMACROS
-# define xasprintf(p, f, args...) \
-    x_asprintf((p), __FILE__, __LINE__, (f), args)
-#else
-# define xasprintf x_asprintf
-#endif
-
-/* Last two arguments are always file and line number.  These are internal
-   implementations that should not be called directly.  ISO C99 says that
-   identifiers beginning with _ and a lowercase letter are reserved for
-   identifiers of file scope, so while the position of libraries in the
-   standard isn't clear, it's probably not entirely kosher to use _xmalloc
-   here.  Use x_malloc instead. */
-extern void *x_calloc(size_t, size_t, const char *, int);
-extern void *x_malloc(size_t, const char *, int);
-extern void *x_realloc(void *, size_t, const char *, int);
-extern char *x_strdup(const char *, const char *, int);
-extern char *x_strndup(const char *, size_t, const char *, int);
-extern int x_vasprintf(char **, const char *, va_list, const char *, int);
-
-/* asprintf special case. */
-#if INN_HAVE_C99_VAMACROS || INN_HAVE_GNU_VAMACROS
-extern int x_asprintf(char **, const char *, int, const char *, ...)
-    __attribute__((__format__(printf, 4, 5)));
-#else
-extern int x_asprintf(char **, const char *, ...)
-    __attribute__((__format__(printf, 2, 3)));
-#endif
-
-/* Failure handler takes the function, the size, the file, and the line. */
-typedef void (*xmalloc_handler_type)(const char *, size_t, const char *, int);
-
-/* The default error handler. */
-void xmalloc_fail(const char *, size_t, const char *, int)
-    __attribute__((__noreturn__));
-
-/* Assign to this variable to choose a handler other than the default, which
-   just calls sysdie. */
-extern xmalloc_handler_type xmalloc_error_handler;
-
-
-/*
 **  TIME AND DATE PARSING, GENERATION, AND HANDLING
 */
 extern int      Argify(char *line, char ***argvp);

Added: include/inn/xmalloc.h
===================================================================
--- include/inn/xmalloc.h	                        (rev 0)
+++ include/inn/xmalloc.h	2014-09-05 19:32:31 UTC (rev 9677)
@@ -0,0 +1,120 @@
+/* $Id$
+ *
+ * Prototypes for malloc routines with failure handling.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Copyright 2010, 2012, 2013, 2014
+ *     The Board of Trustees of the Leland Stanford Junior University
+ * Copyright (c) 2004, 2005, 2006
+ *     by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ *     2002, 2003 by The Internet Software Consortium and Rich Salz
+ *
+ * This code is derived from software contributed to the Internet Software
+ * Consortium by Rich Salz.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef INN_XMALLOC_H
+#define INN_XMALLOC_H 1
+
+#include "config.h"
+
+#include <stdarg.h>
+#include <stddef.h>
+
+/*
+ * The functions are actually macros so that we can pick up the file and line
+ * number information for debugging error messages without the user having to
+ * pass those in every time.
+ */
+#define xcalloc(n, size)        x_calloc((n), (size), __FILE__, __LINE__)
+#define xmalloc(size)           x_malloc((size), __FILE__, __LINE__)
+#define xrealloc(p, size)       x_realloc((p), (size), __FILE__, __LINE__)
+#define xstrdup(p)              x_strdup((p), __FILE__, __LINE__)
+#define xstrndup(p, size)       x_strndup((p), (size), __FILE__, __LINE__)
+#define xvasprintf(p, f, a)     x_vasprintf((p), (f), (a), __FILE__, __LINE__)
+#define xreallocarray(p, n, size) \
+    x_reallocarray((p), (n), (size), __FILE__, __LINE__)
+
+/*
+ * asprintf is a special case since it takes variable arguments.  If we have
+ * support for variadic macros, we can still pass in the file and line and
+ * just need to put them somewhere else in the argument list than last.
+ * Otherwise, just call x_asprintf directly.  This means that the number of
+ * arguments x_asprintf takes must vary depending on whether variadic macros
+ * are supported.
+ */
+#ifdef INN_HAVE_C99_VAMACROS
+# define xasprintf(p, f, ...) \
+    x_asprintf((p), __FILE__, __LINE__, (f), __VA_ARGS__)
+#elif INN_HAVE_GNU_VAMACROS
+# define xasprintf(p, f, args...) \
+    x_asprintf((p), __FILE__, __LINE__, (f), args)
+#else
+# define xasprintf x_asprintf
+#endif
+
+BEGIN_DECLS
+
+/*
+ * Last two arguments are always file and line number.  These are internal
+ * implementations that should not be called directly.
+ */
+void *x_calloc(size_t, size_t, const char *, int)
+    __attribute__((__alloc_size__(1, 2), __malloc__, __nonnull__));
+void *x_malloc(size_t, const char *, int)
+    __attribute__((__alloc_size__(1), __malloc__, __nonnull__));
+void *x_realloc(void *, size_t, const char *, int)
+    __attribute__((__alloc_size__(2), __malloc__, __nonnull__(3)));
+void *x_reallocarray(void *, size_t, size_t, const char *, int)
+    __attribute__((__alloc_size__(2, 3), __malloc__, __nonnull__(4)));
+char *x_strdup(const char *, const char *, int)
+    __attribute__((__malloc__, __nonnull__));
+char *x_strndup(const char *, size_t, const char *, int)
+    __attribute__((__malloc__, __nonnull__));
+void x_vasprintf(char **, const char *, va_list, const char *, int)
+    __attribute__((__nonnull__));
+
+/* asprintf special case. */
+#if INN_HAVE_C99_VAMACROS || INN_HAVE_GNU_VAMACROS
+void x_asprintf(char **, const char *, int, const char *, ...)
+    __attribute__((__nonnull__, __format__(printf, 4, 5)));
+#else
+void x_asprintf(char **, const char *, ...)
+    __attribute__((__nonnull__, __format__(printf, 2, 3)));
+#endif
+
+/*
+ * Failure handler takes the function, the size, the file, and the line.  The
+ * size will be zero if the failure was due to some failure in snprintf
+ * instead of a memory allocation failure.
+ */
+typedef void (*xmalloc_handler_type)(const char *, size_t, const char *, int);
+
+/* The default error handler. */
+void xmalloc_fail(const char *, size_t, const char *, int)
+    __attribute__((__nonnull__));
+
+/*
+ * Assign to this variable to choose a handler other than the default, which
+ * just calls sysdie.
+ */
+extern xmalloc_handler_type xmalloc_error_handler;
+
+END_DECLS
+
+#endif /* INN_XMALLOC_H */


Property changes on: trunk/include/inn/xmalloc.h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: lib/buffer.c
===================================================================
--- lib/buffer.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ lib/buffer.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -47,7 +47,7 @@
 #include <sys/stat.h>
 
 #include "inn/buffer.h"
-#include "inn/libinn.h"
+#include "inn/xmalloc.h"
 
 
 /*

Modified: lib/confparse.c
===================================================================
--- lib/confparse.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ lib/confparse.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -1710,10 +1710,7 @@
     struct config_parameter *param;
 
     va_start(args, fmt);
-    if (xvasprintf(&message, fmt, args) < 0) {
-        va_end(args);
-        return;
-    }
+    xvasprintf(&message, fmt, args);
     va_end(args);
 
     param = hash_lookup(group->params, key);
@@ -1740,10 +1737,7 @@
     char *message;
 
     va_start(args, fmt);
-    if (xvasprintf(&message, fmt, args) < 0) {
-        va_end(args);
-        return;
-    }
+    xvasprintf(&message, fmt, args);
     va_end(args);
     warn("%s:%u: %s", group->file, group->line, message);
     free(message);

Added: lib/reallocarray.c
===================================================================
--- lib/reallocarray.c	                        (rev 0)
+++ lib/reallocarray.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -0,0 +1,57 @@
+/* $Id$
+ *
+ * Replacement for a missing reallocarray.
+ *
+ * Provides the same functionality as the OpenBSD library function reallocarray
+ * for those systems that don't have it.  This function is the same as
+ * realloc, but takes the size arguments in the same form as calloc and checks
+ * for overflow so that the caller doesn't need to.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle at eyrie.org>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+
+#include "config.h"
+#include "clibrary.h"
+
+#include <errno.h>
+
+/*
+ * If we're running the test suite, rename reallocarray to avoid conflicts
+ * with the system version.  #undef it first because some systems may define
+ * it to another name.
+ */
+#if TESTING
+# undef reallocarray
+# define reallocarray test_reallocarray
+void *test_reallocarray(void *, size_t, size_t);
+#endif
+
+/*
+ * nmemb * size cannot overflow if both are smaller than sqrt(SIZE_MAX).  We
+ * can calculate that value statically by using 2^(sizeof(size_t) * 8) as the
+ * value of SIZE_MAX and then taking the square root, which gives
+ * 2^(sizeof(size_t) * 4).  Compute the exponentiation with shift.
+ */
+#define CHECK_THRESHOLD (1UL << (sizeof(size_t) * 4))
+
+void *
+reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+    if (nmemb >= CHECK_THRESHOLD || size >= CHECK_THRESHOLD)
+        if (nmemb > 0 && SIZE_MAX / nmemb <= size) {
+            errno = ENOMEM;
+            return NULL;
+        }
+    return realloc(ptr, nmemb * size);
+}


Property changes on: trunk/lib/reallocarray.c
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: lib/xmalloc.c
===================================================================
--- lib/xmalloc.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ lib/xmalloc.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -1,80 +1,116 @@
 /* $Id$
-**
-**  malloc routines with failure handling.
-**
-**  Usage:
-**
-**       extern xmalloc_handler_t memory_error;
-**       extern const char *string;
-**       char *buffer;
-**       va_list args;
-**
-**       xmalloc_error_handler = memory_error;
-**       buffer = xmalloc(1024);
-**       xrealloc(buffer, 2048);
-**       free(buffer);
-**       buffer = xcalloc(1024);
-**       free(buffer);
-**       buffer = xstrdup(string);
-**       free(buffer);
-**       buffer = xstrndup(string, 25);
-**       free(buffer);
-**       xasprintf(&buffer, "%s", "some string");
-**       free(buffer);
-**       xvasprintf(&buffer, "%s", args);
-**
-**  xmalloc, xcalloc, xrealloc, and xstrdup behave exactly like their C
-**  library counterparts without the leading x except that they will never
-**  return NULL.  Instead, on error, they call xmalloc_error_handler,
-**  passing it the name of the function whose memory allocation failed, the
-**  amount of the allocation, and the file and line number where the
-**  allocation function was invoked (from __FILE__ and __LINE__).  This
-**  function may do whatever it wishes, such as some action to free up
-**  memory or a call to sleep to hope that system resources return.  If the
-**  handler returns, the interrupted memory allocation function will try its
-**  allocation again (calling the handler again if it still fails).
-**
-**  xstrndup behaves like xstrdup but only copies the given number of
-**  characters.  It allocates an additional byte over its second argument and
-**  always nul-terminates the string.
-**
-**  xasprintf and xvasprintf behave just like their GNU glibc library
-**  implementations except that they do the same checking as described above.
-**  xasprintf will only be able to provide accurate file and line information
-**  on systems that support variadic macros.
-**
-**  The default error handler, if none is set by the caller, prints an error
-**  message to stderr and exits with exit status 1.  An error handler must
-**  take a const char * (function name), size_t (bytes allocated), const
-**  char * (file), and int (line).
-**
-**  xmalloc will return a pointer to a valid memory region on an xmalloc of 0
-**  bytes, ensuring this by allocating space for one character instead of 0
-**  bytes.
-**
-**  The functions defined here are actually x_malloc, x_realloc, etc.  The
-**  header file defines macros named xmalloc, etc. that pass the file name
-**  and line number to these functions.
-*/
+ *
+ * malloc routines with failure handling.
+ *
+ * Usage:
+ *
+ *      extern xmalloc_handler_t memory_error;
+ *      extern const char *string;
+ *      char *buffer;
+ *      va_list args;
+ *
+ *      xmalloc_error_handler = memory_error;
+ *      buffer = xmalloc(1024);
+ *      xrealloc(buffer, 2048);
+ *      free(buffer);
+ *      buffer = xcalloc(1024);
+ *      free(buffer);
+ *      buffer = xstrdup(string);
+ *      free(buffer);
+ *      buffer = xstrndup(string, 25);
+ *      free(buffer);
+ *      xasprintf(&buffer, "%s", "some string");
+ *      free(buffer);
+ *      xvasprintf(&buffer, "%s", args);
+ *
+ * xmalloc, xcalloc, xrealloc, and xstrdup behave exactly like their C library
+ * counterparts without the leading x except that they will never return NULL.
+ * Instead, on error, they call xmalloc_error_handler, passing it the name of
+ * the function whose memory allocation failed, the amount of the allocation,
+ * and the file and line number where the allocation function was invoked
+ * (from __FILE__ and __LINE__).  This function may do whatever it wishes,
+ * such as some action to free up memory or a call to sleep to hope that
+ * system resources return.  If the handler returns, the interrupted memory
+ * allocation function will try its allocation again (calling the handler
+ * again if it still fails).
+ *
+ * xreallocarray behaves the same as the OpenBSD reallocarray function but for
+ * the same error checking, which in turn is the same as realloc but with
+ * calloc-style arguments and size overflow checking.
+ *
+ * xstrndup behaves like xstrdup but only copies the given number of
+ * characters.  It allocates an additional byte over its second argument and
+ * always nul-terminates the string.
+ *
+ * xasprintf and xvasprintf behave just like their GNU glibc library
+ * implementations except that they do the same checking as described above.
+ * xasprintf will only be able to provide accurate file and line information
+ * on systems that support variadic macros.
+ *
+ * The default error handler, if none is set by the caller, prints an error
+ * message to stderr and exits with exit status 1.  An error handler must take
+ * a const char * (function name), size_t (bytes allocated), const char *
+ * (file), and int (line).
+ *
+ * xmalloc will return a pointer to a valid memory region on an xmalloc of 0
+ * bytes, ensuring this by allocating space for one character instead of 0
+ * bytes.
+ *
+ * The functions defined here are actually x_malloc, x_realloc, etc.  The
+ * header file defines macros named xmalloc, etc. that pass the file name and
+ * line number to these functions.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Copyright 2012, 2013, 2014
+ *     The Board of Trustees of the Leland Stanford Junior University
+ * Copyright (c) 2004, 2005, 2006
+ *     by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ *     2002, 2003 by The Internet Software Consortium and Rich Salz
+ *
+ * This code is derived from software contributed to the Internet Software
+ * Consortium by Rich Salz.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
 
 #include "config.h"
 #include "clibrary.h"
-#include <errno.h>
 
 #include "inn/messages.h"
-#include "inn/libinn.h"
+#include "inn/xmalloc.h"
 
-/* The default error handler. */
+
+/*
+ * The default error handler.
+ */
 void
 xmalloc_fail(const char *function, size_t size, const char *file, int line)
 {
-    sysdie("failed to %s %lu bytes at %s line %d", function,
-           (unsigned long) size, file, line);
+    if (size == 0)
+        sysdie("failed to format output with %s at %s line %d", function,
+               file, line);
+    else
+        sysdie("failed to %s %lu bytes at %s line %d", function,
+               (unsigned long) size, file, line);
 }
 
 /* Assign to this variable to choose a handler other than the default. */
 xmalloc_handler_type xmalloc_error_handler = xmalloc_fail;
 
+
 void *
 x_malloc(size_t size, const char *file, int line)
 {
@@ -90,6 +126,7 @@
     return p;
 }
 
+
 void *
 x_calloc(size_t n, size_t size, const char *file, int line)
 {
@@ -105,6 +142,7 @@
     return p;
 }
 
+
 void *
 x_realloc(void *p, size_t size, const char *file, int line)
 {
@@ -118,6 +156,21 @@
     return newp;
 }
 
+
+void *
+x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
+{
+    void *newp;
+
+    newp = reallocarray(p, n, size);
+    while (newp == NULL && size > 0 && n > 0) {
+        (*xmalloc_error_handler)("reallocarray", n * size, file, line);
+        newp = reallocarray(p, n, size);
+    }
+    return newp;
+}
+
+
 char *
 x_strdup(const char *s, const char *file, int line)
 {
@@ -134,22 +187,35 @@
     return p;
 }
 
+
+/*
+ * Avoid using the system strndup function since it may not exist (on Mac OS
+ * X, for example), and there's no need to introduce another portability
+ * requirement.
+ */
 char *
 x_strndup(const char *s, size_t size, const char *file, int line)
 {
-    char *p;
+    const char *p;
+    size_t length;
+    char *copy;
 
-    p = malloc(size + 1);
-    while (p == NULL) {
-        (*xmalloc_error_handler)("strndup", size + 1, file, line);
-        p = malloc(size + 1);
+    /* Don't assume that the source string is nul-terminated. */
+    for (p = s; (size_t) (p - s) < size && *p != '\0'; p++)
+        ;
+    length = p - s;
+    copy = malloc(length + 1);
+    while (copy == NULL) {
+        (*xmalloc_error_handler)("strndup", length + 1, file, line);
+        copy = malloc(length + 1);
     }
-    memcpy(p, s, size);
-    p[size] = '\0';
-    return p;
+    memcpy(copy, s, length);
+    copy[length] = '\0';
+    return copy;
 }
 
-int
+
+void
 x_vasprintf(char **strp, const char *fmt, va_list args, const char *file,
             int line)
 {
@@ -159,20 +225,21 @@
     va_copy(args_copy, args);
     status = vasprintf(strp, fmt, args_copy);
     va_end(args_copy);
-    while (status < 0 && errno == ENOMEM) {
+    while (status < 0) {
         va_copy(args_copy, args);
         status = vsnprintf(NULL, 0, fmt, args_copy);
         va_end(args_copy);
-        (*xmalloc_error_handler)("vasprintf", status + 1, file, line);
+        status = (status < 0) ? 0 : status + 1;
+        (*xmalloc_error_handler)("vasprintf", status, file, line);
         va_copy(args_copy, args);
         status = vasprintf(strp, fmt, args_copy);
         va_end(args_copy);
     }
-    return status;
 }
 
+
 #if INN_HAVE_C99_VAMACROS || INN_HAVE_GNU_VAMACROS
-int
+void
 x_asprintf(char **strp, const char *file, int line, const char *fmt, ...)
 {
     va_list args, args_copy;
@@ -182,19 +249,19 @@
     va_copy(args_copy, args);
     status = vasprintf(strp, fmt, args_copy);
     va_end(args_copy);
-    while (status < 0 && errno == ENOMEM) {
+    while (status < 0) {
         va_copy(args_copy, args);
         status = vsnprintf(NULL, 0, fmt, args_copy);
         va_end(args_copy);
-        (*xmalloc_error_handler)("asprintf", status + 1, file, line);
+        status = (status < 0) ? 0 : status + 1;
+        (*xmalloc_error_handler)("asprintf", status, file, line);
         va_copy(args_copy, args);
         status = vasprintf(strp, fmt, args_copy);
         va_end(args_copy);
     }
-    return status;
 }
 #else /* !(INN_HAVE_C99_VAMACROS || INN_HAVE_GNU_VAMACROS) */
-int
+void
 x_asprintf(char **strp, const char *fmt, ...)
 {
     va_list args, args_copy;
@@ -204,15 +271,15 @@
     va_copy(args_copy, args);
     status = vasprintf(strp, fmt, args_copy);
     va_end(args_copy);
-    while (status < 0 && errno == ENOMEM) {
+    while (status < 0) {
         va_copy(args_copy, args);
         status = vsnprintf(NULL, 0, fmt, args_copy);
         va_end(args_copy);
-        (*xmalloc_error_handler)("asprintf", status + 1, __FILE__, __LINE__);
+        status = (status < 0) ? 0 : status + 1;
+        (*xmalloc_error_handler)("asprintf", status, __FILE__, __LINE__);
         va_copy(args_copy, args);
         status = vasprintf(strp, fmt, args_copy);
         va_end(args_copy);
     }
-    return status;
 }
 #endif /* !(INN_HAVE_C99_VAMACROS || INN_HAVE_GNU_VAMACROS) */

Modified: tests/Makefile
===================================================================
--- tests/Makefile	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/Makefile	2014-09-05 19:32:31 UTC (rev 9677)
@@ -21,7 +21,8 @@
 	lib/hashtab.t lib/hex.t lib/inet_aton.t \
 	lib/inet_ntoa.t lib/inet_ntop.t lib/innconf.t lib/list.t lib/md5.t \
 	lib/memcmp.t lib/messages.t lib/mkstemp.t lib/network.t lib/pread.t \
-	lib/pwrite.t lib/qio.t lib/setenv.t lib/snprintf.t lib/strlcat.t \
+	lib/pwrite.t lib/qio.t lib/reallocarray.t \
+	lib/setenv.t lib/snprintf.t lib/strlcat.t \
 	lib/strlcpy.t lib/tst.t lib/uwildmat.t lib/vector.t lib/wire.t \
 	lib/xwrite.t nnrpd/auth-ext.t overview/api.t overview/buffindexed.t \
 	overview/tradindexed.t overview/xref.t util/innbind.t
@@ -193,6 +194,12 @@
 lib/qio.t: lib/qio-t.o tap/basic.o $(LIBINN)
 	$(LINK) lib/qio-t.o tap/basic.o $(LIBINN)
 
+lib/reallocarray.o: ../lib/reallocarray.c
+	$(CC) $(CFLAGS) -DTESTING -c -o $@ ../lib/reallocarray.c
+
+lib/reallocarray.t: lib/reallocarray.o lib/reallocarray-t.o tap/basic.o $(LIBINN)
+	$(LINK) lib/reallocarray.o lib/reallocarray-t.o tap/basic.o $(LIBINN)
+
 lib/setenv.o: ../lib/setenv.c
 	$(CC) $(CFLAGS) -DTESTING -c -o $@ ../lib/setenv.c
 

Modified: tests/TESTS
===================================================================
--- tests/TESTS	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/TESTS	2014-09-05 19:32:31 UTC (rev 9677)
@@ -29,6 +29,7 @@
 lib/pread
 lib/pwrite
 lib/qio
+lib/reallocarray
 lib/setenv
 lib/snprintf
 lib/strlcat


Property changes on: trunk/tests/lib
___________________________________________________________________
Modified: svn:ignore
   - .libs
.pure
asprintf.t
buffer.t
concat.t
conffile.t
confparse.t
date.t
dispatch.t
getaddrinfo.t
getnameinfo.t
hash.t
hashtab.t
hex.t
hstrerror.t
inet_aton.t
inet_ntoa.t
inet_ntop.t
innconf.t
list.t
md5.t
memcmp.t
messages.t
mkstemp.t
network.t
pread.t
pwrite.t
qio.t
setenv.t
snprintf.t
strlcat.t
strlcpy.t
tst.t
uwildmat.t
vector.t
wire.t
xmalloc
xwrite.t

   + .libs
.pure
asprintf.t
buffer.t
concat.t
conffile.t
confparse.t
date.t
dispatch.t
getaddrinfo.t
getnameinfo.t
hash.t
hashtab.t
hex.t
hstrerror.t
inet_aton.t
inet_ntoa.t
inet_ntop.t
innconf.t
list.t
md5.t
memcmp.t
messages.t
mkstemp.t
network.t
pread.t
pwrite.t
qio.t
reallocarray.t
setenv.t
snprintf.t
strlcat.t
strlcpy.t
tst.t
uwildmat.t
vector.t
wire.t
xmalloc
xwrite.t


Modified: tests/lib/getnameinfo-t.c
===================================================================
--- tests/lib/getnameinfo-t.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/lib/getnameinfo-t.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -23,7 +23,7 @@
 #include "portable/socket.h"
 
 #include "tap/basic.h"
-#include "inn/libinn.h"
+#include "inn/xmalloc.h"
 
 int test_getnameinfo(const struct sockaddr *, socklen_t, char *, socklen_t,
                      char *, socklen_t, int);

Modified: tests/lib/messages-t.c
===================================================================
--- tests/lib/messages-t.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/lib/messages-t.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -112,8 +112,7 @@
 
     /* Now, check the results against what we expected. */
     va_start(args, format);
-    if (xvasprintf(&msg, format, args) < 0)
-        bail("cannot format test description");
+    xvasprintf(&msg, format, args);
     va_end(args);
     ok(WIFEXITED(rval), "%s (exited)", msg);
     is_int(status, WEXITSTATUS(rval), "%s (status)", msg);

Added: tests/lib/reallocarray-t.c
===================================================================
--- tests/lib/reallocarray-t.c	                        (rev 0)
+++ tests/lib/reallocarray-t.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -0,0 +1,88 @@
+/* $Id$
+ *
+ * reallocarray test suite.
+ *
+ * This does some simple sanity checks and checks some of the overflow
+ * detection, but isn't particularly thorough.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle at eyrie.org>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+
+#define LIBTEST_NEW_FORMAT 1
+
+#include "config.h"
+#include "clibrary.h"
+
+#include <errno.h>
+
+#include "tap/basic.h"
+
+void *test_reallocarray(void *, size_t, size_t);
+
+
+int
+main(void)
+{
+    char *p, *base;
+    size_t sqrt_max;
+
+    plan(15);
+
+    /* Test success cases and write to the memory for valgrind checks. */
+    p = test_reallocarray(NULL, 2, 5);
+    memcpy(p, "123456789", 10);
+    is_string("123456789", p, "reallocarray of NULL");
+    p = test_reallocarray(p, 4, 5);
+    is_string("123456789", p, "reallocarray after resize");
+    memcpy(p + 9, "0123456789", 11);
+    is_string("1234567890123456789", p, "write to larger memory segment");
+    free(p);
+
+    /*
+     * If nmemb or size are 0, we should either get NULL or a pointer we can
+     * free.  Make sure we don't get something weird, like division by zero.
+     */
+    p = test_reallocarray(NULL, 0, 100);
+    if (p != NULL)
+        free(p);
+    p = test_reallocarray(NULL, 100, 0);
+    if (p != NULL)
+        free(p);
+
+    /* Test the range-checking error cases. */
+    p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
+    ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
+    is_int(ENOMEM, errno, "...with correct errno");
+    base = malloc(10);
+    p = test_reallocarray(base, 3, SIZE_MAX / 3);
+    ok(p == NULL, "reallocarray fails for 3, SIZE_MAX / 3");
+    is_int(ENOMEM, errno, "...with correct errno");
+    sqrt_max = (1UL << (sizeof(size_t) * 4));
+    p = test_reallocarray(base, sqrt_max, sqrt_max);
+    ok(p == NULL, "reallocarray fails for sqrt(SIZE_MAX), sqrt(SIZE_MAX)");
+    is_int(ENOMEM, errno, "...with correct errno");
+    p = test_reallocarray(base, 1, SIZE_MAX);
+    ok(p == NULL, "reallocarray fails for 1, SIZE_MAX");
+    is_int(ENOMEM, errno, "...with correct errno");
+    p = test_reallocarray(base, SIZE_MAX, 1);
+    ok(p == NULL, "reallocarray fails for SIZE_MAX, 1");
+    is_int(ENOMEM, errno, "...with correct errno");
+    p = test_reallocarray(base, 2, SIZE_MAX);
+    ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
+    is_int(ENOMEM, errno, "...with correct errno");
+
+    /* Clean up and exit. */
+    free(base);
+    return 0;
+}


Property changes on: trunk/tests/lib/reallocarray-t.c
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: tests/lib/vector-t.c
===================================================================
--- tests/lib/vector-t.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/lib/vector-t.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -26,7 +26,7 @@
 #include "tap/basic.h"
 #include "tap/string.h"
 #include "inn/vector.h"
-#include "inn/libinn.h"
+#include "inn/xmalloc.h"
 
 
 int

Modified: tests/lib/xmalloc.c
===================================================================
--- tests/lib/xmalloc.c	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/lib/xmalloc.c	2014-09-05 19:32:31 UTC (rev 9677)
@@ -1,13 +1,12 @@
-/*
+/* $Id$
+ *
  * Test suite for xmalloc and family.
  *
- * $Id$
- *
  * The canonical version of this file is maintained in the rra-c-util package,
  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
- * Copyright 2000, 2001, 2006 Russ Allbery <rra at stanford.edu>
- * Copyright 2008
+ * Copyright 2000, 2001, 2006 Russ Allbery <eagle at eyrie.org>
+ * Copyright 2008, 2012, 2013, 2014
  *     The Board of Trustees of the Leland Stanford Junior University
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -31,23 +30,21 @@
 
 #define LIBTEST_NEW_FORMAT 1
 
+#line 1 "xmalloc.c"
+
 #include "config.h"
 #include "clibrary.h"
+
 #include <ctype.h>
 #include <errno.h>
-#include <unistd.h>
+#include "portable/time.h"
 
 /* Linux requires sys/time.h be included before sys/resource.h. */
-#if HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
 #include <sys/resource.h>
 
 #include "inn/messages.h"
-#include "inn/libinn.h"
+#include "inn/xmalloc.h"
 
-/* Adjust the beginning of the test file. */
-#line 15 "xmalloc.c"
 
 /*
  * A customized error handler for checking xmalloc's support of them.  Prints
@@ -85,9 +82,9 @@
 
 
 /*
- * Allocate 10 bytes, write to it, then reallocate to the desired
- * size, writing to the rest and then checking it all.  Returns true on
- * success, false on any failure.
+ * Allocate 10 bytes of memory given, write to it, then reallocate to the
+ * desired size, writing to the rest and then checking it all.  Returns true
+ * on success, false on any failure.
  */
 static int
 test_realloc(size_t size)
@@ -116,6 +113,36 @@
 
 
 /*
+ * Like test_realloc, but test allocating an array instead.  Returns true on
+ * success, false on any failure.
+ */
+static int
+test_reallocarray(size_t n, size_t size)
+{
+    char *buffer;
+    size_t i;
+
+    buffer = xmalloc(10);
+    if (buffer == NULL)
+        return 0;
+    memset(buffer, 1, 10);
+    buffer = xreallocarray(buffer, n, size);
+    if (buffer == NULL)
+        return 0;
+    if (n > 0 && size > 0)
+        memset(buffer + 10, 2, (n * size) - 10);
+    for (i = 0; i < 10; i++)
+        if (buffer[i] != 1)
+            return 0;
+    for (i = 10; i < n * size; i++)
+        if (buffer[i] != 2)
+            return 0;
+    free(buffer);
+    return 1;
+}
+
+
+/*
  * Generate a string of the size indicated, call xstrdup on it, and then
  * ensure the result matches.  Returns true on success, false on any failure.
  */
@@ -142,15 +169,34 @@
 
 /*
  * Generate a string of the size indicated plus some, call xstrndup on it, and
- * then ensure the result matches.  Returns true on success, false on any
- * failure.
+ * then ensure the result matches.  Also test xstrdup on a string that's
+ * shorter than the specified size and ensure that we don't copy too much, and
+ * on a string that's not nul-terminated.  Returns true on success, false on
+ * any failure.
  */
 static int
 test_strndup(size_t size)
 {
     char *string, *copy;
-    int match, toomuch;
+    int shortmatch, nonulmatch, match, toomuch;
 
+    /* Copy a short string. */
+    string = xmalloc(5);
+    memcpy(string, "test", 5);
+    copy = xstrndup(string, size);
+    shortmatch = strcmp(string, copy);
+    free(string);
+    free(copy);
+
+    /* Copy a string that's not nul-terminated. */
+    string = xmalloc(4);
+    memcpy(string, "test", 4);
+    copy = xstrndup(string, 4);
+    nonulmatch = strcmp(copy, "test");
+    free(string);
+    free(copy);
+
+    /* Now the test of running out of memory. */
     string = xmalloc(size + 1);
     if (string == NULL)
         return 0;
@@ -164,7 +210,7 @@
     toomuch = strcmp(string, copy);
     free(string);
     free(copy);
-    return (match == 0 && toomuch != 0);
+    return (shortmatch == 0 && nonulmatch == 0 && match == 0 && toomuch != 0);
 }
 
 
@@ -200,16 +246,13 @@
 test_asprintf(size_t size)
 {
     char *copy, *string;
-    int status;
     size_t i;
 
     string = xmalloc(size);
     memset(string, 42, size - 1);
     string[size - 1] = '\0';
-    status = xasprintf(&copy, "%s", string);
+    xasprintf(&copy, "%s", string);
     free(string);
-    if (status < 0)
-        return 0;
     for (i = 0; i < size - 1; i++)
         if (copy[i] != 42)
             return 0;
@@ -221,16 +264,14 @@
 
 
 /* Wrapper around vasprintf to do the va_list stuff. */
-static int
+static void
 xvasprintf_wrapper(char **strp, const char *format, ...)
 {
     va_list args;
-    int status;
 
     va_start(args, format);
-    status = xvasprintf(strp, format, args);
+    xvasprintf(strp, format, args);
     va_end(args);
-    return status;
 }
 
 
@@ -242,16 +283,13 @@
 test_vasprintf(size_t size)
 {
     char *copy, *string;
-    int status;
     size_t i;
 
     string = xmalloc(size);
     memset(string, 42, size - 1);
     string[size - 1] = '\0';
-    status = xvasprintf_wrapper(&copy, "%s", string);
+    xvasprintf_wrapper(&copy, "%s", string);
     free(string);
-    if (status < 0)
-        return 0;
     for (i = 0; i < size - 1; i++)
         if (copy[i] != 42)
             return 0;
@@ -317,6 +355,7 @@
 #if HAVE_SETRLIMIT && defined(RLIMIT_AS)
         struct rlimit rl;
         void *tmp;
+        size_t test_size;
 
         rl.rlim_cur = limit;
         rl.rlim_max = limit;
@@ -324,11 +363,14 @@
             syswarn("Can't set data limit to %lu", (unsigned long) limit);
             exit(2);
         }
-        if (size < limit || code == 'r') {
-            tmp = malloc(code == 'r' ? 10 : size);
+        if (size < limit || code == 'r' || code == 'y') {
+            test_size = (code == 'r' || code == 'y') ? 10 : size;
+            if (test_size == 0)
+                test_size = 1;
+            tmp = malloc(test_size);
             if (tmp == NULL) {
-                syswarn("Can't allocate initial memory of %lu",
-                        (unsigned long) size);
+                syswarn("Can't allocate initial memory of %lu (limit %lu)",
+                        (unsigned long) test_size, (unsigned long) limit);
                 exit(2);
             }
             free(tmp);
@@ -343,6 +385,7 @@
     case 'c': exit(test_calloc(size) ? willfail : 1);
     case 'm': exit(test_malloc(size) ? willfail : 1);
     case 'r': exit(test_realloc(size) ? willfail : 1);
+    case 'y': exit(test_reallocarray(4, size / 4) ? willfail : 1);
     case 's': exit(test_strdup(size) ? willfail : 1);
     case 'n': exit(test_strndup(size) ? willfail : 1);
     case 'a': exit(test_asprintf(size) ? willfail : 1);

Modified: tests/lib/xmalloc.t
===================================================================
--- tests/lib/xmalloc.t	2014-09-05 18:47:24 UTC (rev 9676)
+++ tests/lib/xmalloc.t	2014-09-05 19:32:31 UTC (rev 9677)
@@ -1,15 +1,15 @@
 #! /bin/sh
 #
+# $Id$
+#
 # Test suite for xmalloc and friends.
 #
-# $Id$
-#
 # The canonical version of this file is maintained in the rra-c-util package,
 # which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
 #
-# Written by Russ Allbery <rra at stanford.edu>
-# Copyright 2000, 2001, 2006 Russ Allbery <rra at stanford.edu>
-# Copyright 2008, 2009, 2010
+# Written by Russ Allbery <eagle at eyrie.org>
+# Copyright 2000, 2001, 2006, 2014 Russ Allbery <eagle at eyrie.org>
+# Copyright 2008, 2009, 2010, 2012
 #     The Board of Trustees of the Leland Stanford Junior University
 #
 # Permission is hereby granted, free of charge, to any person obtaining a
@@ -43,18 +43,16 @@
     shift
     w_output="$1"
     shift
-    output=`./xmalloc "$@" 2>&1`
+    output=`strip_colon_error ./xmalloc "$@" 2>&1`
     status=$?
-    if [ "$w_status" -ne 0 ] ; then
-        output=`echo "$output" | sed 's/:.*//'`
-    fi
     if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then
         ok "$desc" true
     elif [ $status = 2 ] ; then
+        diag "$output"
         skip "no data limit support"
     else
-        echo "#  saw: ($status) $output"
-        echo "#  not: ($w_status) $w_output"
+        diag "saw: ($status) $output"
+        diag "not: ($w_status) $w_output"
         ok "$desc" false
     fi
 }
@@ -63,84 +61,94 @@
 # failures in automated testing have been problems with the assumptions around
 # memory allocation or problems with the test suite, not problems with the
 # underlying xmalloc code.
-#if [ -z "$RRA_MAINTAINER_TESTS" ] ; then
-#    skip_all 'xmalloc tests only run for maintainer'
-#fi
+if [ -z "$AUTHOR_TESTING" ] ; then
+    skip_all 'xmalloc tests only run for author'
+fi
 
 # Total tests.
-plan 36
+plan 41
 
 # First run the tests expected to succeed.
-ok_xmalloc "malloc small"    0 "" "m" "21"      "0"
-ok_xmalloc "malloc large"    0 "" "m" "3500000" "0"
-ok_xmalloc "malloc zero"     0 "" "m" "0"       "0"
-ok_xmalloc "realloc small"   0 "" "r" "21"      "0"
-ok_xmalloc "realloc large"   0 "" "r" "3500000" "0"
-ok_xmalloc "strdup small"    0 "" "s" "21"      "0"
-ok_xmalloc "strdup large"    0 "" "s" "3500000" "0"
-ok_xmalloc "strndup small"   0 "" "n" "21"      "0"
-ok_xmalloc "strndup large"   0 "" "n" "3500000" "0"
-ok_xmalloc "calloc small"    0 "" "c" "24"      "0"
-ok_xmalloc "calloc large"    0 "" "c" "3500000" "0"
-ok_xmalloc "asprintf small"  0 "" "a" "24"      "0"
-ok_xmalloc "asprintf large"  0 "" "a" "3500000" "0"
-ok_xmalloc "vasprintf small" 0 "" "v" "24"      "0"
-ok_xmalloc "vasprintf large" 0 "" "v" "3500000" "0"
+ok_xmalloc "malloc small"       0 "" "m" "21"       "0"
+ok_xmalloc "malloc large"       0 "" "m" "30000000" "0"
+ok_xmalloc "malloc zero"        0 "" "m" "0"        "0"
+ok_xmalloc "realloc small"      0 "" "r" "21"       "0"
+ok_xmalloc "realloc large"      0 "" "r" "30000000" "0"
+ok_xmalloc "reallocarray small" 0 "" "y" "20"       "0"
+ok_xmalloc "reallocarray large" 0 "" "y" "30000000" "0"
+ok_xmalloc "strdup small"       0 "" "s" "21"       "0"
+ok_xmalloc "strdup large"       0 "" "s" "30000000" "0"
+ok_xmalloc "strndup small"      0 "" "n" "21"       "0"
+ok_xmalloc "strndup large"      0 "" "n" "30000000" "0"
+ok_xmalloc "calloc small"       0 "" "c" "24"       "0"
+ok_xmalloc "calloc large"       0 "" "c" "30000000" "0"
+ok_xmalloc "asprintf small"     0 "" "a" "24"       "0"
+ok_xmalloc "asprintf large"     0 "" "a" "30000000" "0"
+ok_xmalloc "vasprintf small"    0 "" "v" "24"       "0"
+ok_xmalloc "vasprintf large"    0 "" "v" "30000000" "0"
 
-# Now limit our memory to 3.5MB and then try the large ones again, all of
+# Now limit our memory to 30MB and then try the large ones again, all of
 # which should fail.
 #
 # The exact memory limits used here are essentially black magic.  They need to
 # be large enough to allow the program to be loaded and do small allocations,
 # but not so large that we can't reasonably expect to allocate that much
-# memory normally.  3.5MB seems to work reasonably well on both Solaris and
-# Linux.
+# memory normally.  The amount of memory required varies a lot based on what
+# shared libraries are loaded, and if it's too small, all memory allocations
+# fail.  30MB seems to work reasonably well on both Solaris and Linux, even
+# when the program is linked with additional libraries.
 #
 # We assume that there are enough miscellaneous allocations that an allocation
 # exactly as large as the limit will always fail.
 ok_xmalloc "malloc fail" 1 \
-    "failed to malloc 3500000 bytes at xmalloc.c line 38" \
-    "m" "3500000" "3500000"
+    "failed to malloc 30000000 bytes at xmalloc.c line 38" \
+    "m" "30000000" "30000000"
 ok_xmalloc "realloc fail" 1 \
-    "failed to realloc 3500000 bytes at xmalloc.c line 66" \
-    "r" "3500000" "3500000"
+    "failed to realloc 30000000 bytes at xmalloc.c line 66" \
+    "r" "30000000" "30000000"
+ok_xmalloc "reallocarray fail" 1 \
+    "failed to reallocarray 30000000 bytes at xmalloc.c line 96" \
+    "y" "30000000" "30000000"
 ok_xmalloc "strdup fail" 1 \
-    "failed to strdup 3500000 bytes at xmalloc.c line 97" \
-    "s" "3500000" "3500000"
+    "failed to strdup 30000000 bytes at xmalloc.c line 127" \
+    "s" "30000000" "30000000"
 ok_xmalloc "strndup fail" 1 \
-    "failed to strndup 3500000 bytes at xmalloc.c line 124" \
-    "n" "3500000" "3500000"
+    "failed to strndup 30000000 bytes at xmalloc.c line 173" \
+    "n" "30000000" "30000000"
 ok_xmalloc "calloc fail" 1 \
-    "failed to calloc 3500000 bytes at xmalloc.c line 148" \
-    "c" "3500000" "3500000"
+    "failed to calloc 30000000 bytes at xmalloc.c line 197" \
+    "c" "30000000" "30000000"
 ok_xmalloc "asprintf fail" 1 \
-    "failed to asprintf 3500000 bytes at xmalloc.c line 173" \
-    "a" "3500000" "3500000"
+    "failed to asprintf 30000000 bytes at xmalloc.c line 221" \
+    "a" "30000000" "30000000"
 ok_xmalloc "vasprintf fail" 1 \
-    "failed to vasprintf 3500000 bytes at xmalloc.c line 195" \
-    "v" "3500000" "3500000"
+    "failed to vasprintf 30000000 bytes at xmalloc.c line 240" \
+    "v" "30000000" "30000000"
 
 # Check our custom error handler.
-ok_xmalloc "malloc custom"    1 "malloc 3500000 xmalloc.c 38" \
-    "M" "3500000" "3500000"
-ok_xmalloc "realloc custom"   1 "realloc 3500000 xmalloc.c 66" \
-    "R" "3500000" "3500000"
-ok_xmalloc "strdup custom"    1 "strdup 3500000 xmalloc.c 97" \
-    "S" "3500000" "3500000"
-ok_xmalloc "strndup custom"   1 "strndup 3500000 xmalloc.c 124" \
-    "N" "3500000" "3500000"
-ok_xmalloc "calloc custom"    1 "calloc 3500000 xmalloc.c 148" \
-    "C" "3500000" "3500000"
-ok_xmalloc "asprintf custom"  1 "asprintf 3500000 xmalloc.c 173" \
-    "A" "3500000" "3500000"
-ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 195" \
-    "V" "3500000" "3500000"
+ok_xmalloc "malloc custom"       1 "malloc 30000000 xmalloc.c 38" \
+    "M" "30000000" "30000000"
+ok_xmalloc "realloc custom"      1 "realloc 30000000 xmalloc.c 66" \
+    "R" "30000000" "30000000"
+ok_xmalloc "reallocarray custom" 1 "reallocarray 30000000 xmalloc.c 96" \
+    "Y" "30000000" "30000000"
+ok_xmalloc "strdup custom"       1 "strdup 30000000 xmalloc.c 127" \
+    "S" "30000000" "30000000"
+ok_xmalloc "strndup custom"      1 "strndup 30000000 xmalloc.c 173" \
+    "N" "30000000" "30000000"
+ok_xmalloc "calloc custom"       1 "calloc 30000000 xmalloc.c 197" \
+    "C" "30000000" "30000000"
+ok_xmalloc "asprintf custom"     1 "asprintf 30000000 xmalloc.c 221" \
+    "A" "30000000" "30000000"
+ok_xmalloc "vasprintf custom"    1 "vasprintf 30000000 xmalloc.c 240" \
+    "V" "30000000" "30000000"
 
 # Check the smaller ones again just for grins.
-ok_xmalloc "malloc retry"    0 "" "m" "21" "3500000"
-ok_xmalloc "realloc retry"   0 "" "r" "32" "3500000"
-ok_xmalloc "strdup retry"    0 "" "s" "64" "3500000"
-ok_xmalloc "strndup retry"   0 "" "n" "20" "3500000"
-ok_xmalloc "calloc retry"    0 "" "c" "24" "3500000"
-ok_xmalloc "asprintf retry"  0 "" "a" "30" "3500000"
-ok_xmalloc "vasprintf retry" 0 "" "v" "35" "3500000"
+ok_xmalloc "malloc retry"       0 "" "m" "21" "30000000"
+ok_xmalloc "realloc retry"      0 "" "r" "32" "30000000"
+ok_xmalloc "reallocarray retry" 0 "" "y" "32" "30000000"
+ok_xmalloc "strdup retry"       0 "" "s" "64" "30000000"
+ok_xmalloc "strndup retry"      0 "" "n" "20" "30000000"
+ok_xmalloc "calloc retry"       0 "" "c" "24" "30000000"
+ok_xmalloc "asprintf retry"     0 "" "a" "30" "30000000"
+ok_xmalloc "vasprintf retry"    0 "" "v" "35" "30000000"



More information about the inn-committers mailing list