Commit 0ae048d4 authored by Russ Allbery's avatar Russ Allbery

Add further changes from rra-c-util 4.6

Drop concat and concatpath from the util library.  Change xasprintf,
xvasprintf, basprintf, and bvasprintf to be void functions and remove
any remaining checks of their exit status.
parent 67c8cb9c
......@@ -20,9 +20,9 @@ portable_libportable_la_SOURCES = portable/dummy.c portable/krb5-extra.c \
portable/system.h
portable_libportable_la_LDFLAGS = $(KRB5_LDFLAGS)
portable_libportable_la_LIBADD = $(LTLIBOBJS) $(KRB5_LIBS)
util_libutil_la_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_la_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_la_LIBADD = portable/libportable.la $(KRB5_LIBS)
# Put the module into /usr/local/lib/krb5/plugins by default, relative to
......@@ -87,11 +87,10 @@ warnings:
$(MAKE) V=0 CFLAGS='$(WARNINGS)' $(check_PROGRAMS)
# The bits below are for the test suite, not for the main package.
check_PROGRAMS = tests/runtests tests/portable/asprintf-t \
tests/portable/snprintf-t tests/portable/strlcat-t \
tests/portable/strlcpy-t tests/portable/strndup-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/snprintf-t tests/portable/strlcat-t \
tests/portable/strlcpy-t tests/portable/strndup-t \
tests/util/messages-krb5-t tests/util/messages-t tests/util/xmalloc
check_LIBRARIES = tests/tap/libtap.a
tests_runtests_CPPFLAGS = -DSOURCE='"$(abs_top_srcdir)/tests"' \
-DBUILD='"$(abs_top_builddir)/tests"'
......@@ -117,8 +116,6 @@ tests_portable_strlcpy_t_LDADD = tests/tap/libtap.a portable/libportable.la
tests_portable_strndup_t_SOURCES = tests/portable/strndup-t.c \
tests/portable/strndup.c
tests_portable_strndup_t_LDADD = tests/tap/libtap.a portable/libportable.la
tests_util_concat_t_LDADD = tests/tap/libtap.a util/libutil.la \
portable/libportable.la
tests_util_messages_krb5_t_LDFLAGS = $(KRB5_LDFLAGS)
tests_util_messages_krb5_t_LDADD = tests/tap/libtap.a util/libutil.la \
portable/libportable.la $(KRB5_LIBS)
......
......@@ -5,7 +5,6 @@ portable/snprintf
portable/strlcat
portable/strlcpy
portable/strndup
util/concat
util/messages
util/messages-krb5
util/xmalloc
......@@ -9,7 +9,7 @@
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Copyright 2002, 2004, 2005 Russ Allbery <rra@stanford.edu>
* Copyright 2006, 2007, 2009
* Copyright 2006, 2007, 2009, 2012
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
......@@ -34,11 +34,10 @@
#include <config.h>
#include <portable/system.h>
#include <tests/tap/macros.h>
#include <tests/tap/messages.h>
#include <util/concat.h>
#include <util/macros.h>
#include <tests/tap/string.h>
#include <util/messages.h>
#include <util/xmalloc.h>
/* A global buffer into which message_log_buffer stores error messages. */
char *errors = NULL;
......@@ -49,18 +48,18 @@ char *errors = NULL;
* error_capture.
*/
static void
message_log_buffer(int len, const char *fmt, va_list args, int error UNUSED)
message_log_buffer(int len UNUSED, const char *fmt, va_list args,
int error UNUSED)
{
char *message;
message = xmalloc(len + 1);
vsnprintf(message, len + 1, fmt, args);
if (errors == NULL) {
errors = concat(message, "\n", (char *) 0);
} else {
bvasprintf(&message, fmt, args);
if (errors == NULL)
basprintf(&errors, "%s\n", message);
else {
char *new_errors;
new_errors = concat(errors, message, "\n", (char *) 0);
basprintf(&new_errors, "%s%s\n", errors, message);
free(errors);
errors = new_errors;
}
......
......@@ -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/>.
*
* Copyright 2011 Russ Allbery <rra@stanford.edu>
* Copyright 2011, 2012 Russ Allbery <rra@stanford.edu>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -39,7 +39,7 @@
* vsprintf into a newly allocated string, reporting a fatal error with bail
* on failure.
*/
int
void
bvasprintf(char **strp, const char *fmt, va_list args)
{
int status;
......@@ -47,7 +47,6 @@ bvasprintf(char **strp, const char *fmt, va_list args)
status = vasprintf(strp, fmt, args);
if (status < 0)
sysbail("failed to allocate memory for vasprintf");
return status;
}
......@@ -55,14 +54,12 @@ bvasprintf(char **strp, const char *fmt, va_list args)
* sprintf into a newly allocated string, reporting a fatal error with bail on
* failure.
*/
int
void
basprintf(char **strp, const char *fmt, ...)
{
va_list args;
int status;
va_start(args, fmt);
status = bvasprintf(strp, fmt, args);
bvasprintf(strp, fmt, args);
va_end(args);
return status;
}
......@@ -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/>.
*
* Copyright 2011 Russ Allbery <rra@stanford.edu>
* Copyright 2011, 2012 Russ Allbery <rra@stanford.edu>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -38,10 +38,10 @@
BEGIN_DECLS
/* sprintf into an allocated string, calling bail on allocation failure. */
int basprintf(char **, const char *, ...)
/* sprintf into an allocated string, calling bail on any failure. */
void basprintf(char **, const char *, ...)
__attribute__((__nonnull__, __format__(printf, 2, 3)));
int bvasprintf(char **, const char *, va_list)
void bvasprintf(char **, const char *, va_list)
__attribute__((__nonnull__));
END_DECLS
......
/*
* concat test suite.
*
* 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 <tests/tap/basic.h>
#include <util/concat.h>
#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;
}
......@@ -6,7 +6,7 @@
*
* Written by Russ Allbery <rra@stanford.edu>
* Copyright 2002, 2004, 2005 Russ Allbery <rra@stanford.edu>
* Copyright 2009, 2010, 2011
* Copyright 2009, 2010, 2011, 2012
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
......@@ -38,7 +38,6 @@
#include <tests/tap/basic.h>
#include <tests/tap/process.h>
#include <util/concat.h>
#include <util/macros.h>
#include <util/messages.h>
#include <util/xmalloc.h>
......@@ -166,7 +165,7 @@ test_strerror(int status, const char *output, int error,
{
char *full_output, *name;
full_output = concat(output, ": ", strerror(error), "\n", (char *) NULL);
xasprintf(&full_output, "%s: %s\n", output, strerror(error));
xasprintf(&name, "strerror %lu", testnum / 3 + 1);
is_function_output(function, NULL, status, full_output, "%s", name);
free(full_output);
......
......@@ -110,10 +110,10 @@ ok_xmalloc "calloc fail" 1 \
"failed to calloc 3500000 bytes at xmalloc.c line 167" \
"c" "3500000" "3500000"
ok_xmalloc "asprintf fail" 1 \
"failed to asprintf 3500000 bytes at xmalloc.c line 192" \
"failed to asprintf 3500000 bytes at xmalloc.c line 191" \
"a" "3500000" "3500000"
ok_xmalloc "vasprintf fail" 1 \
"failed to vasprintf 3500000 bytes at xmalloc.c line 214" \
"failed to vasprintf 3500000 bytes at xmalloc.c line 210" \
"v" "3500000" "3500000"
# Check our custom error handler.
......@@ -127,9 +127,9 @@ ok_xmalloc "strndup custom" 1 "strndup 3500000 xmalloc.c 143" \
"N" "3500000" "3500000"
ok_xmalloc "calloc custom" 1 "calloc 3500000 xmalloc.c 167" \
"C" "3500000" "3500000"
ok_xmalloc "asprintf custom" 1 "asprintf 3500000 xmalloc.c 192" \
ok_xmalloc "asprintf custom" 1 "asprintf 3500000 xmalloc.c 191" \
"A" "3500000" "3500000"
ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 214" \
ok_xmalloc "vasprintf custom" 1 "vasprintf 3500000 xmalloc.c 210" \
"V" "3500000" "3500000"
# Check the smaller ones again just for grins.
......
......@@ -213,16 +213,13 @@ static int
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;
......@@ -234,16 +231,14 @@ test_asprintf(size_t size)
/* 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;
}
......@@ -255,16 +250,13 @@ static int
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;
......
/*
* 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);
}
/*
* 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 */
......@@ -54,8 +54,7 @@ die_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
if (ctx != NULL)
k5_msg = krb5_get_error_message(ctx, code);
va_start(args, format);
if (xvasprintf(&message, format, args) < 0)
die("internal error: unable to format error message");
xvasprintf(&message, format, args);
va_end(args);
if (k5_msg == NULL)
die("%s", message);
......@@ -77,8 +76,7 @@ warn_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
if (ctx != NULL)
k5_msg = krb5_get_error_message(ctx, code);
va_start(args, format);
if (xvasprintf(&message, format, args) < 0)
die("internal error: unable to format error message");
xvasprintf(&message, format, args);
va_end(args);
if (k5_msg == NULL)
warn("%s", message);
......
......@@ -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) */
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment