From d05f66dbff10b525d37f60ee01d5b9f94bf5192e Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Tue, 9 Feb 2010 16:00:04 -0800 Subject: Update util code and import Kerberos portability glue Use the Kerberos portability layer from rra-c-util 3.0 and avoid Kerberos API calls deprecated on Heimdal. Break util/util.h into separate header files and update all source files accordingly. The test suite is not yet updated. That will come in subsequent commits. --- util/concat.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 util/concat.h (limited to 'util/concat.h') diff --git a/util/concat.h b/util/concat.h new file mode 100644 index 0000000..ef8b38d --- /dev/null +++ b/util/concat.h @@ -0,0 +1,36 @@ +/* + * Prototypes for string concatenation with dynamic memory allocation. + * + * Written by Russ Allbery + * This work is hereby placed in the public domain by its author. + */ + +#ifndef UTIL_CONCAT_H +#define UTIL_CONCAT_H 1 + +#include +#include + +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 */ -- cgit v1.2.3 From 93e566f6f9ae8a767d2188ad1fb1520c9c2d303a Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Wed, 27 Feb 2013 14:54:05 -0800 Subject: Drop use of concat in favor of xasprintf Change-Id: I6a84920b0c0dc1849af8a34ecf8f3fb70b45e17c Reviewed-on: https://gerrit.stanford.edu/843 Reviewed-by: Russ Allbery Tested-by: Russ Allbery --- .gitignore | 1 - Makefile.am | 18 +++++------ client/file.c | 5 ++- client/keytab.c | 9 +++--- tests/TESTS | 1 - tests/util/concat-t.c | 46 ---------------------------- util/concat.c | 85 --------------------------------------------------- util/concat.h | 36 ---------------------- 8 files changed, 14 insertions(+), 187 deletions(-) delete mode 100644 tests/util/concat-t.c delete mode 100644 util/concat.c delete mode 100644 util/concat.h (limited to 'util/concat.h') diff --git a/.gitignore b/.gitignore index 23ffe53..21fba4a 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,6 @@ /tests/portable/strlcat-t /tests/portable/strlcpy-t /tests/runtests -/tests/util/concat-t /tests/util/messages-krb5-t /tests/util/messages-t /tests/util/xmalloc diff --git a/Makefile.am b/Makefile.am index 772a71e..fa70b4f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,9 +56,9 @@ portable_libportable_a_SOURCES = portable/dummy.c portable/krb5-extra.c \ portable/system.h portable/uio.h portable_libportable_a_CPPFLAGS = $(KRB5_CPPFLAGS) portable_libportable_a_LIBADD = $(LIBOBJS) -util_libutil_a_SOURCES = util/concat.c util/concat.h util/macros.h \ - util/messages-krb5.c util/messages-krb5.h util/messages.c \ - util/messages.h util/xmalloc.c util/xmalloc.h +util_libutil_a_SOURCES = util/macros.h util/messages-krb5.c \ + util/messages-krb5.h util/messages.c util/messages.h util/xmalloc.c \ + util/xmalloc.h util_libutil_a_CPPFLAGS = $(KRB5_CPPFLAGS) noinst_LIBRARIES += client/libwallet.a @@ -155,11 +155,11 @@ distclean-local: fi # The bits below are for the test suite, not for the main package. -check_PROGRAMS = tests/runtests tests/portable/asprintf-t \ - tests/portable/mkstemp-t tests/portable/setenv-t \ - tests/portable/snprintf-t tests/portable/strlcat-t \ - tests/portable/strlcpy-t tests/util/concat-t \ - tests/util/messages-krb5-t tests/util/messages-t tests/util/xmalloc +check_PROGRAMS = tests/runtests tests/portable/asprintf-t \ + tests/portable/mkstemp-t tests/portable/setenv-t \ + tests/portable/snprintf-t tests/portable/strlcat-t \ + tests/portable/strlcpy-t tests/util/messages-krb5-t \ + tests/util/messages-t tests/util/xmalloc tests_runtests_CPPFLAGS = -DSOURCE='"$(abs_top_srcdir)/tests"' \ -DBUILD='"$(abs_top_builddir)/tests"' check_LIBRARIES = tests/tap/libtap.a @@ -188,8 +188,6 @@ tests_portable_strlcat_t_LDADD = tests/tap/libtap.a portable/libportable.a tests_portable_strlcpy_t_SOURCES = tests/portable/strlcpy-t.c \ tests/portable/strlcpy.c tests_portable_strlcpy_t_LDADD = tests/tap/libtap.a portable/libportable.a -tests_util_concat_t_LDADD = tests/tap/libtap.a util/libutil.a \ - portable/libportable.a tests_util_messages_krb5_t_CPPFLAGS = $(KRB5_CPPFLAGS) tests_util_messages_krb5_t_LDFLAGS = $(KRB5_LDFLAGS) tests_util_messages_krb5_t_LDADD = tests/tap/libtap.a util/libutil.a \ diff --git a/client/file.c b/client/file.c index c171969..511c995 100644 --- a/client/file.c +++ b/client/file.c @@ -16,7 +16,6 @@ #include #include -#include #include #include @@ -83,8 +82,8 @@ write_file(const char *name, const void *data, size_t length) { char *temp, *backup; - temp = concat(name, ".new", (char *) 0); - backup = concat(name, ".bak", (char *) 0); + xasprintf(&temp, "%s.new", name); + xasprintf(&backup, "%s.bak", name); overwrite_file(temp, data, length); if (access(name, F_OK) == 0) { if (access(backup, F_OK) == 0) diff --git a/client/keytab.c b/client/keytab.c index 0a3e419..d7106e1 100644 --- a/client/keytab.c +++ b/client/keytab.c @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -107,7 +106,7 @@ merge_keytab(krb5_context ctx, const char *newfile, const char *file) krb5_error_code status; memset(&entry, 0, sizeof(entry)); - oldfile = concat("WRFILE:", file, (char *) 0); + xasprintf(&oldfile, "WRFILE:%s", file); status = krb5_kt_resolve(ctx, oldfile, &old); if (status != 0) die_krb5(ctx, status, "cannot open keytab %s", file); @@ -189,7 +188,7 @@ get_keytab(struct remctl *r, krb5_context ctx, const char *type, return 255; } if (access(file, F_OK) == 0) { - tempfile = concat(file, ".new", (char *) 0); + xasprintf(&tempfile, "%s.new", file); overwrite_file(tempfile, data, length); if (srvtab != NULL) write_srvtab(ctx, srvtab, name, tempfile); @@ -225,7 +224,7 @@ rekey_keytab(struct remctl *r, krb5_context ctx, const char *type, bool error = false, rekeyed = false; struct principal_name *names, *current; - tempfile = concat(file, ".new", (char *) 0); + xasprintf(&tempfile, "%s.new", file); krb5_get_default_realm(ctx, &realm); names = keytab_principals(ctx, file, realm); for (current = names; current != NULL; current = current->next) { @@ -260,7 +259,7 @@ rekey_keytab(struct remctl *r, krb5_context ctx, const char *type, } else { if (error) { data = read_file(file, &length); - backupfile = concat(file, ".old", (char *) 0); + xasprintf(&backupfile, "%s.old", file); overwrite_file(backupfile, data, length); warn("partial failure to rekey keytab %s, old keytab left in %s", file, backupfile); diff --git a/tests/TESTS b/tests/TESTS index 54b8190..807d944 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -13,7 +13,6 @@ portable/strlcpy server/admin server/backend server/keytab -util/concat util/messages util/messages-krb5 util/xmalloc diff --git a/tests/util/concat-t.c b/tests/util/concat-t.c deleted file mode 100644 index ca7de2c..0000000 --- a/tests/util/concat-t.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * concat test suite. - * - * Written by Russ Allbery - * Copyright 2009 Board of Trustees, Leland Stanford Jr. 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 - * - * See LICENSE for licensing terms. - */ - -#include -#include - -#include -#include - -#define END (char *) 0 - -/* - * Memory leaks everywhere! Whoo-hoo! - */ -int -main(void) -{ - plan(13); - - is_string("a", concat("a", END), "concat 1"); - is_string("ab", concat("a", "b", END), "concat 2"); - is_string("ab", concat("ab", "", END), "concat 3"); - is_string("ab", concat("", "ab", END), "concat 4"); - is_string("", concat("", END), "concat 5"); - is_string("abcde", concat("ab", "c", "", "de", END), "concat 6"); - is_string("abcde", concat("abc", "de", END, "f", END), "concat 7"); - - is_string("/foo", concatpath("/bar", "/foo"), "path 1"); - is_string("/foo/bar", concatpath("/foo", "bar"), "path 2"); - is_string("./bar", concatpath("/foo", "./bar"), "path 3"); - is_string("/bar/baz/foo/bar", concatpath("/bar/baz", "foo/bar"), "path 4"); - is_string("./foo", concatpath(NULL, "foo"), "path 5"); - is_string("/foo/bar", concatpath(NULL, "/foo/bar"), "path 6"); - - return 0; -} diff --git a/util/concat.c b/util/concat.c deleted file mode 100644 index bdbd836..0000000 --- a/util/concat.c +++ /dev/null @@ -1,85 +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. - * - * Written by Russ Allbery - * This work is hereby placed in the public domain by its author. - */ - -#include -#include - -#include -#include - -/* 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 ef8b38d..0000000 --- a/util/concat.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Prototypes for string concatenation with dynamic memory allocation. - * - * Written by Russ Allbery - * This work is hereby placed in the public domain by its author. - */ - -#ifndef UTIL_CONCAT_H -#define UTIL_CONCAT_H 1 - -#include -#include - -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 */ -- cgit v1.2.3