From: Vincent Bernat Date: Tue, 1 Apr 2014 16:29:07 +0000 (+0200) Subject: asprintf: globally define _GNU_SOURCE X-Git-Tag: 0.7.8~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=46673df2cbbe93fc38c3c08ee27cd5ded7dd6eef;p=thirdparty%2Flldpd.git asprintf: globally define _GNU_SOURCE And also provide a replacement for asprintf if it is not available. I would prefer to not use _GNU_SOURCE everywhere but it seems cleaner than putting it at the top of random files. Ideally, I should wrap every function in a separate file to just use _GNU_SOURCE for those functions. There are `asprintf()` and `setres[ug]id()`. --- diff --git a/configure.ac b/configure.ac index eefab1d6..4ccd6ba3 100644 --- a/configure.ac +++ b/configure.ac @@ -76,6 +76,7 @@ AX_CFLAGS_GCC_OPTION([-Winline]) AX_CFLAGS_GCC_OPTION([-fstack-protector]) AX_CFLAGS_GCC_OPTION([-fno-omit-frame-pointer]) AX_CFLAGS_GCC_OPTION([-D_FORTIFY_SOURCE=2]) +AX_CFLAGS_GCC_OPTION([-D_GNU_SOURCE=1]) AX_CFLAGS_GCC_OPTION([-Wno-unused-parameter]) AX_CFLAGS_GCC_OPTION([-Wno-missing-field-initializers]) AX_CFLAGS_GCC_OPTION([-Wno-sign-compare]) dnl Should be fixed later @@ -106,7 +107,7 @@ AC_FUNC_FORK AC_SEARCH_LIBS([setproctitle], [util bsd]) AC_REPLACE_FUNCS([setproctitle]) AC_CHECK_FUNCS([setproctitle_init]) -AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln]) +AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln asprintf]) AC_CHECK_FUNCS([setresuid setresgid]) case " $LIBS " in diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index 35c1b94d..be3d3622 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -16,7 +16,6 @@ */ -#define _GNU_SOURCE #include #include #include diff --git a/src/compat/asprintf.c b/src/compat/asprintf.c new file mode 100644 index 00000000..4fd3f94c --- /dev/null +++ b/src/compat/asprintf.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2004 Darren Tucker. + * + * Based originally on asprintf.c from OpenBSD: + * Copyright (c) 1997 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#define INIT_SZ 128 + +static int _vasprintf(char **str, const char *fmt, va_list ap) +{ + int ret = -1; + va_list ap2; + char *string, *newstr; + size_t len; + + va_copy(ap2, ap); + if ((string = malloc(INIT_SZ)) == NULL) + goto fail; + + ret = vsnprintf(string, INIT_SZ, fmt, ap2); + if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ + *str = string; + } else if (ret == INT_MAX || ret < 0) { /* Bad length */ + free(string); + goto fail; + } else { /* bigger than initial, realloc allowing for nul */ + len = (size_t)ret + 1; + if ((newstr = realloc(string, len)) == NULL) { + free(string); + goto fail; + } else { + va_end(ap2); + va_copy(ap2, ap); + ret = vsnprintf(newstr, len, fmt, ap2); + if (ret >= 0 && (size_t)ret < len) { + *str = newstr; + } else { /* failed with realloc'ed string, give up */ + free(newstr); + goto fail; + } + } + } + va_end(ap2); + return (ret); + +fail: + *str = NULL; + errno = ENOMEM; + va_end(ap2); + return (-1); +} + +int asprintf(char **str, const char *fmt, ...) +{ + va_list ap; + int ret; + + *str = NULL; + va_start(ap, fmt); + ret = _vasprintf(str, fmt, ap); + va_end(ap); + + return ret; +} diff --git a/src/compat/compat.h b/src/compat/compat.h index cf623fca..e4cc969a 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -45,6 +45,10 @@ # include #endif +#if !HAVE_ASPRINTF +int asprintf (char **, const char *, ...) __attribute__ ((format (printf, 2, 3))); +#endif + #if !HAVE_STRLCPY size_t strlcpy(char *, const char *, size_t); #endif diff --git a/src/daemon/lldpd.h b/src/daemon/lldpd.h index fa66636a..f2b5f1f4 100644 --- a/src/daemon/lldpd.h +++ b/src/daemon/lldpd.h @@ -17,7 +17,6 @@ #ifndef _LLDPD_H #define _LLDPD_H -#define _GNU_SOURCE 1 #if HAVE_CONFIG_H # include diff --git a/src/lib/fixedpoint.c b/src/lib/fixedpoint.c index 7c99eef9..155fad11 100644 --- a/src/lib/fixedpoint.c +++ b/src/lib/fixedpoint.c @@ -15,7 +15,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _GNU_SOURCE 1 #include #include #include diff --git a/src/log.c b/src/log.c index 7bb128c4..5a1db221 100644 --- a/src/log.c +++ b/src/log.c @@ -17,7 +17,6 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _GNU_SOURCE #include #include #include diff --git a/src/marshal.c b/src/marshal.c index 943eb893..16dabe57 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -15,7 +15,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _GNU_SOURCE 1 #define MARSHAL_EXPORT #include "marshal.h" diff --git a/tests/check_cdp.c b/tests/check_cdp.c index cf6ce13f..237fce18 100644 --- a/tests/check_cdp.c +++ b/tests/check_cdp.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE 1 #include #include #include diff --git a/tests/check_edp.c b/tests/check_edp.c index 0f9905dd..040d7820 100644 --- a/tests/check_edp.c +++ b/tests/check_edp.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE 1 #include #include #include diff --git a/tests/check_lldp.c b/tests/check_lldp.c index b5d2a782..1b48372b 100644 --- a/tests/check_lldp.c +++ b/tests/check_lldp.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE 1 #include #include #include diff --git a/tests/check_sonmp.c b/tests/check_sonmp.c index 596618d3..b3cf7319 100644 --- a/tests/check_sonmp.c +++ b/tests/check_sonmp.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE 1 #include #include #include diff --git a/tests/common.c b/tests/common.c index 45869982..e1ecc707 100644 --- a/tests/common.c +++ b/tests/common.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE 1 #include #include #include