diff options
Diffstat (limited to 'portable')
-rw-r--r-- | portable/asprintf.c | 10 | ||||
-rw-r--r-- | portable/dummy.c | 8 | ||||
-rw-r--r-- | portable/krb5-extra.c | 2 | ||||
-rw-r--r-- | portable/krb5.h | 4 | ||||
-rw-r--r-- | portable/macros.h | 2 | ||||
-rw-r--r-- | portable/mkstemp.c | 2 | ||||
-rw-r--r-- | portable/reallocarray.c | 56 | ||||
-rw-r--r-- | portable/setenv.c | 2 | ||||
-rw-r--r-- | portable/snprintf.c | 11 | ||||
-rw-r--r-- | portable/stdbool.h | 2 | ||||
-rw-r--r-- | portable/strlcat.c | 2 | ||||
-rw-r--r-- | portable/strlcpy.c | 2 | ||||
-rw-r--r-- | portable/system.h | 26 | ||||
-rw-r--r-- | portable/uio.h | 2 |
14 files changed, 102 insertions, 29 deletions
diff --git a/portable/asprintf.c b/portable/asprintf.c index 0093070..eb2b713 100644 --- a/portable/asprintf.c +++ b/portable/asprintf.c @@ -7,7 +7,7 @@ * 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@stanford.edu> + * 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 @@ -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 50052ec..890bc0c 100644 --- a/portable/dummy.c +++ b/portable/dummy.c @@ -8,7 +8,7 @@ * 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@stanford.edu> + * 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 @@ -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-extra.c b/portable/krb5-extra.c index 849842c..b1c8b8d 100644 --- a/portable/krb5-extra.c +++ b/portable/krb5-extra.c @@ -9,7 +9,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/krb5.h b/portable/krb5.h index b418ae7..6dfffd5 100644 --- a/portable/krb5.h +++ b/portable/krb5.h @@ -19,7 +19,7 @@ * 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@stanford.edu> + * 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 @@ -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/macros.h b/portable/macros.h index b33d064..b5093f5 100644 --- a/portable/macros.h +++ b/portable/macros.h @@ -4,7 +4,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/mkstemp.c b/portable/mkstemp.c index 8668db1..2cbfe08 100644 --- a/portable/mkstemp.c +++ b/portable/mkstemp.c @@ -7,7 +7,7 @@ * 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@stanford.edu> + * 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 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/setenv.c b/portable/setenv.c index fd2b10c..472282a 100644 --- a/portable/setenv.c +++ b/portable/setenv.c @@ -7,7 +7,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/snprintf.c b/portable/snprintf.c index 91c8491..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 @@ -73,7 +74,7 @@ * fixed handling of %.0f * added test for HAVE_LONG_DOUBLE * - * Russ Allbery <rra@stanford.edu> 2000-08-26 + * Russ Allbery <eagle@eyrie.org> 2000-08-26 * fixed return value to comply with C99 * fixed handling of snprintf(NULL, ...) * @@ -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/stdbool.h b/portable/stdbool.h index 045436f..14d011b 100644 --- a/portable/stdbool.h +++ b/portable/stdbool.h @@ -8,7 +8,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/strlcat.c b/portable/strlcat.c index 3bee4ee..9c8686d 100644 --- a/portable/strlcat.c +++ b/portable/strlcat.c @@ -12,7 +12,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/strlcpy.c b/portable/strlcpy.c index df75fd8..592e3ee 100644 --- a/portable/strlcpy.c +++ b/portable/strlcpy.c @@ -11,7 +11,7 @@ * 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@stanford.edu> + * 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 diff --git a/portable/system.h b/portable/system.h index d1ccc94..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 @@ -22,7 +24,7 @@ * 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@stanford.edu> + * 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 @@ -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 diff --git a/portable/uio.h b/portable/uio.h index 3bd1f96..2192f8c 100644 --- a/portable/uio.h +++ b/portable/uio.h @@ -8,7 +8,7 @@ * 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@stanford.edu> + * 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 |