diff options
author | Russ Allbery <rra@stanford.edu> | 2012-09-24 18:39:01 -0700 |
---|---|---|
committer | Russ Allbery <rra@stanford.edu> | 2012-09-24 18:39:01 -0700 |
commit | 278348b6e22922fd41d92a62224d293b05244573 (patch) | |
tree | 3fdb592ea030ea14c00fb621f76adb886b91b854 /util | |
parent | f26f6dd275526a46de02fab35466707979208d58 (diff) |
Update to rra-c-util 4.6
* Drop concat from the util library in favor of asprintf.
* Fail on any error in [bx]asprintf and [bx]vasprintf.
Change-Id: Ife0797f7b735f759abb21d330438da729551b78e
Diffstat (limited to 'util')
-rw-r--r-- | util/concat.c | 95 | ||||
-rw-r--r-- | util/concat.h | 46 | ||||
-rw-r--r-- | util/gss-errors.c | 25 | ||||
-rw-r--r-- | util/xmalloc.c | 17 | ||||
-rw-r--r-- | util/xmalloc.h | 8 |
5 files changed, 28 insertions, 163 deletions
diff --git a/util/concat.c b/util/concat.c deleted file mode 100644 index fb4afa8..0000000 --- a/util/concat.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Concatenate strings with dynamic memory allocation. - * - * Usage: - * - * string = concat(string1, string2, ..., (char *) 0); - * path = concatpath(base, name); - * - * Dynamically allocates (using xmalloc) sufficient memory to hold all of the - * strings given and then concatenates them together into that allocated - * memory, returning a pointer to it. Caller is responsible for freeing. - * Assumes xmalloc is available. The last argument must be a null pointer (to - * a char *, if you actually find a platform where it matters). - * - * concatpath is similar, except that it only takes two arguments. If the - * second argument begins with / or ./, a copy of it is returned; otherwise, - * the first argument, a slash, and the second argument are concatenated - * together and returned. This is useful for building file names where names - * that aren't fully qualified are qualified with some particular directory. - * - * 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> - * - * 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 <util/concat.h> -#include <util/xmalloc.h> - -/* Abbreviation for cleaner code. */ -#define VA_NEXT(var, type) ((var) = (type) va_arg(args, type)) - - -/* - * Concatenate all of the arguments into a newly allocated string. ANSI C - * requires at least one named parameter, but it's not treated any different - * than the rest. - */ -char * -concat(const char *first, ...) -{ - va_list args; - char *result, *p; - const char *string; - size_t length = 0; - - /* Find the total memory required. */ - va_start(args, first); - for (string = first; string != NULL; VA_NEXT(string, const char *)) - length += strlen(string); - va_end(args); - length++; - - /* - * Create the string. Doing the copy ourselves avoids useless string - * traversals of result, if using strcat, or string, if using strlen to - * increment a pointer into result, at the cost of losing the native - * optimization of strcat if any. - */ - result = xmalloc(length); - p = result; - va_start(args, first); - for (string = first; string != NULL; VA_NEXT(string, const char *)) - while (*string != '\0') - *p++ = *string++; - va_end(args); - *p = '\0'; - - return result; -} - - -/* - * Concatenate name with base, unless name begins with / or ./. Return the - * new string in newly allocated memory. - */ -char * -concatpath(const char *base, const char *name) -{ - if (name[0] == '/' || (name[0] == '.' && name[1] == '/')) - return xstrdup(name); - else - return concat(base != NULL ? base : ".", "/", name, (char *) 0); -} diff --git a/util/concat.h b/util/concat.h deleted file mode 100644 index 960f242..0000000 --- a/util/concat.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Prototypes for string concatenation with dynamic memory allocation. - * - * 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> - * - * 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. - */ - -#ifndef UTIL_CONCAT_H -#define UTIL_CONCAT_H 1 - -#include <config.h> -#include <portable/macros.h> - -BEGIN_DECLS - -/* Default to a hidden visibility for all util functions. */ -#pragma GCC visibility push(hidden) - -/* Concatenate NULL-terminated strings into a newly allocated string. */ -char *concat(const char *first, ...) - __attribute__((__malloc__, __nonnull__(1))); - -/* - * Given a base path and a file name, create a newly allocated path string. - * The name will be appended to base with a / between them. Exceptionally, if - * name begins with a slash, it will be strdup'd and returned as-is. - */ -char *concatpath(const char *base, const char *name) - __attribute__((__malloc__, __nonnull__(2))); - -/* Undo default visibility change. */ -#pragma GCC visibility pop - -END_DECLS - -#endif /* UTIL_CONCAT_H */ diff --git a/util/gss-errors.c b/util/gss-errors.c index a28044c..7253176 100644 --- a/util/gss-errors.c +++ b/util/gss-errors.c @@ -15,7 +15,6 @@ #include <portable/system.h> #include <portable/gssapi.h> -#include <util/concat.h> #include <util/gss-errors.h> @@ -24,6 +23,9 @@ * "GSS-API error" and the provided string. Uses gss_display_status to get * the internal error message. Returns a newly allocated string that the * caller must free. + * + * Don't use xmalloc here, since this may be inside the client library and die + * inside a library isn't friendly. */ char * gssapi_error_string(const char *prefix, OM_uint32 major, OM_uint32 minor) @@ -38,13 +40,16 @@ gssapi_error_string(const char *prefix, OM_uint32 major, OM_uint32 minor) gss_display_status(&status, major, GSS_C_GSS_CODE, (const gss_OID) GSS_KRB5_MECHANISM, &msg_ctx, &msg); - if (string != NULL) { - old = string; - string = concat(string, ", ", msg.value, (char *) 0); - free(old); + if (string == NULL) { + if (asprintf(&string, "GSS-API error %s: %s", prefix, + (char *) msg.value) < 0) + string = NULL; } else { - string = concat("GSS-API error ", prefix, ": ", msg.value, - (char *) 0); + old = string; + if (asprintf(&string, "%s, %s", old, (char *) msg.value) < 0) + string = old; + else + free(old); } } while (msg_ctx != 0); if (minor != 0) { @@ -54,8 +59,10 @@ gssapi_error_string(const char *prefix, OM_uint32 major, OM_uint32 minor) (const gss_OID) GSS_KRB5_MECHANISM, &msg_ctx, &msg); old = string; - string = concat(string, ", ", msg.value, (char *) 0); - free(old); + if (asprintf(&string, "%s, %s", old, (char *) msg.value) < 0) + string = old; + else + free(old); } while (msg_ctx != 0); } return string; diff --git a/util/xmalloc.c b/util/xmalloc.c index a636941..a78e31a 100644 --- a/util/xmalloc.c +++ b/util/xmalloc.c @@ -58,6 +58,8 @@ * 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 + * 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, @@ -192,7 +194,7 @@ x_strndup(const char *s, size_t size, const char *file, int line) } -int +void x_vasprintf(char **strp, const char *fmt, va_list args, const char *file, int line) { @@ -202,7 +204,7 @@ x_vasprintf(char **strp, const char *fmt, va_list args, const char *file, 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); @@ -211,12 +213,11 @@ x_vasprintf(char **strp, const char *fmt, va_list args, const char *file, status = vasprintf(strp, fmt, args_copy); va_end(args_copy); } - return status; } #if HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS -int +void x_asprintf(char **strp, const char *file, int line, const char *fmt, ...) { va_list args, args_copy; @@ -226,7 +227,7 @@ x_asprintf(char **strp, const char *file, int line, const char *fmt, ...) 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); @@ -235,10 +236,9 @@ x_asprintf(char **strp, const char *file, int line, const char *fmt, ...) status = vasprintf(strp, fmt, args_copy); va_end(args_copy); } - return status; } #else /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */ -int +void x_asprintf(char **strp, const char *fmt, ...) { va_list args, args_copy; @@ -248,7 +248,7 @@ x_asprintf(char **strp, const char *fmt, ...) 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); @@ -257,6 +257,5 @@ x_asprintf(char **strp, const char *fmt, ...) status = vasprintf(strp, fmt, args_copy); va_end(args_copy); } - return status; } #endif /* !(HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS) */ diff --git a/util/xmalloc.h b/util/xmalloc.h index 14d8831..55a0b91 100644 --- a/util/xmalloc.h +++ b/util/xmalloc.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/>. * - * Copyright 2010 + * Copyright 2010, 2012 * The Board of Trustees of the Leland Stanford Junior University * Copyright (c) 2004, 2005, 2006 * by Internet Systems Consortium, Inc. ("ISC") @@ -84,15 +84,15 @@ char *x_strdup(const char *, const char *, int) __attribute__((__malloc__, __nonnull__)); char *x_strndup(const char *, size_t, const char *, int) __attribute__((__malloc__, __nonnull__)); -int x_vasprintf(char **, const char *, va_list, const char *, int) +void x_vasprintf(char **, const char *, va_list, const char *, int) __attribute__((__nonnull__)); /* asprintf special case. */ #if HAVE_C99_VAMACROS || HAVE_GNU_VAMACROS -int x_asprintf(char **, const char *, int, const char *, ...) +void x_asprintf(char **, const char *, int, const char *, ...) __attribute__((__nonnull__, __format__(printf, 4, 5))); #else -int x_asprintf(char **, const char *, ...) +void x_asprintf(char **, const char *, ...) __attribute__((__nonnull__, __format__(printf, 2, 3))); #endif |