summaryrefslogtreecommitdiff
path: root/portable
diff options
context:
space:
mode:
authorRuss Allbery <eagle@eyrie.org>2014-07-11 20:18:41 -0700
committerRuss Allbery <rra@stanford.edu>2014-07-11 22:38:49 -0700
commitda0aba21779529d98436e42323fc12f702390969 (patch)
tree950e33ac99f2ff45303e939bf74f8bfbbb635215 /portable
parent2971570d0e90bd166d87eff14e9e42c095c9f614 (diff)
Update to rra-c-util 5.5 and C TAP Harness 3.1
Update to rra-c-util 5.5: * Use Lancaster Consensus environment variables to control tests. * Use calloc or reallocarray for protection against integer overflows. * Suppress warnings from Kerberos headers in non-system paths. * Assume calloc initializes pointers to NULL. * Assume free(NULL) is properly ignored. * Improve error handling in xasprintf and xvasprintf. * Check the return status of snprintf and vsnprintf properly. * Preserve errno if snprintf fails in vasprintf replacement. Update to C TAP Harness 3.1: * Reopen standard input to /dev/null when running a test list. * Don't leak extraneous file descriptors to tests. * Suppress lazy plans and test summaries if the test failed with bail. * runtests now treats the command line as a list of tests by default. * The full test executable path can now be passed to runtests -o. * Improved harness output for tests with lazy plans. * Improved harness output to a terminal for some abort cases. * Flush harness output after each test even when not on a terminal. Change-Id: I05161eb3d3be49a98f7762e876cb114da0c84e9a Reviewed-on: https://gerrit.stanford.edu/1529 Reviewed-by: Russ Allbery <rra@stanford.edu> Tested-by: Russ Allbery <rra@stanford.edu>
Diffstat (limited to 'portable')
-rw-r--r--portable/asprintf.c8
-rw-r--r--portable/dummy.c6
-rw-r--r--portable/krb5.h2
-rw-r--r--portable/reallocarray.c56
-rw-r--r--portable/snprintf.c9
-rw-r--r--portable/system.h24
6 files changed, 89 insertions, 16 deletions
diff --git a/portable/asprintf.c b/portable/asprintf.c
index 7bdfd0d..eb2b713 100644
--- a/portable/asprintf.c
+++ b/portable/asprintf.c
@@ -21,6 +21,8 @@
#include <config.h>
#include <portable/system.h>
+#include <errno.h>
+
/*
* If we're running the test suite, rename the functions to avoid conflicts
* with the system versions.
@@ -33,6 +35,7 @@ int test_asprintf(char **, const char *, ...)
int test_vasprintf(char **, const char *, va_list);
#endif
+
int
asprintf(char **strp, const char *fmt, ...)
{
@@ -45,11 +48,12 @@ asprintf(char **strp, const char *fmt, ...)
return status;
}
+
int
vasprintf(char **strp, const char *fmt, va_list args)
{
va_list args_copy;
- int status, needed;
+ int status, needed, oerrno;
va_copy(args_copy, args);
needed = vsnprintf(NULL, 0, fmt, args_copy);
@@ -65,8 +69,10 @@ vasprintf(char **strp, const char *fmt, va_list args)
if (status >= 0)
return status;
else {
+ oerrno = errno;
free(*strp);
*strp = NULL;
+ errno = oerrno;
return status;
}
}
diff --git a/portable/dummy.c b/portable/dummy.c
index f2ac917..890bc0c 100644
--- a/portable/dummy.c
+++ b/portable/dummy.c
@@ -19,8 +19,10 @@
* work.
*/
-/* Prototype to avoid gcc warnings. */
-int portable_dummy(void);
+#include <portable/macros.h>
+
+/* Prototype to avoid gcc warnings and set visibility. */
+int portable_dummy(void) __attribute__((__visibility__("hidden")));
int
portable_dummy(void)
diff --git a/portable/krb5.h b/portable/krb5.h
index a3cb173..6dfffd5 100644
--- a/portable/krb5.h
+++ b/portable/krb5.h
@@ -113,4 +113,6 @@ const char *krb5_principal_get_realm(krb5_context, krb5_const_principal);
/* Undo default visibility change. */
#pragma GCC visibility pop
+END_DECLS
+
#endif /* !PORTABLE_KRB5_H */
diff --git a/portable/reallocarray.c b/portable/reallocarray.c
new file mode 100644
index 0000000..7d40a7a
--- /dev/null
+++ b/portable/reallocarray.c
@@ -0,0 +1,56 @@
+/*
+ * Replacement for a missing reallocarray.
+ *
+ * Provides the same functionality as the OpenBSD library function reallocrray
+ * 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@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 <portable/system.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);
+}
diff --git a/portable/snprintf.c b/portable/snprintf.c
index 225455b..c35ad80 100644
--- a/portable/snprintf.c
+++ b/portable/snprintf.c
@@ -2,8 +2,9 @@
* Replacement for a missing snprintf or vsnprintf.
*
* The following implementation of snprintf was taken mostly verbatim from
- * <http://www.fiction.net/~blong/programs/>; it is the version of snprintf
- * used in Mutt.
+ * <http://www.fiction.net/blong/programs/>; it is the version of snprintf
+ * used in Mutt. A possibly newer version is used in wget, found at
+ * <https://github.com/wertarbyte/wget/blob/master/src/snprintf.c>.
*
* Please do not reformat or otherwise change this file more than necessary so
* that later merges with the original source are easy. Bug fixes and
@@ -432,7 +433,7 @@ static int dopr (char *buffer, size_t maxlen, const char *format, va_list args)
break;
case 'w':
/* not supported yet, treat as next char */
- ch = *format++;
+ format++;
break;
default:
/* Unknown, skip */
@@ -695,7 +696,7 @@ static int fmtfp (char *buffer, size_t *currlen, size_t maxlen,
/* For each leading 0 in fractional part, print one more
fractional digit. */
LDOUBLE temp;
- if (ufvalue != 0)
+ if (ufvalue > 0)
for (temp = ufvalue; temp < 0.1; temp *= 10)
++max;
}
diff --git a/portable/system.h b/portable/system.h
index 3be86dd..544b2de 100644
--- a/portable/system.h
+++ b/portable/system.h
@@ -5,15 +5,17 @@
* file is the equivalent of including all of the following headers,
* portably:
*
- * #include <sys/types.h>
+ * #include <inttypes.h>
+ * #include <limits.h>
* #include <stdarg.h>
* #include <stdbool.h>
+ * #include <stddef.h>
* #include <stdio.h>
* #include <stdlib.h>
- * #include <stddef.h>
* #include <stdint.h>
* #include <string.h>
* #include <strings.h>
+ * #include <sys/types.h>
* #include <unistd.h>
*
* Missing functions are provided via #define or prototyped if available from
@@ -43,21 +45,22 @@
#include <portable/macros.h>
/* A set of standard ANSI C headers. We don't care about pre-ANSI systems. */
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
+#if HAVE_STDINT_H
+# include <stdint.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
-#include <sys/types.h>
#include <string.h>
#if HAVE_STRINGS_H
# include <strings.h>
#endif
-#if HAVE_INTTYPES_H
-# include <inttypes.h>
-#endif
-#if HAVE_STDINT_H
-# include <stdint.h>
-#endif
+#include <sys/types.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
@@ -127,6 +130,9 @@ extern int vsnprintf(char *, size_t, const char *, va_list);
#if !HAVE_MKSTEMP
extern int mkstemp(char *);
#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