From f27708ce206f97896bcf001d00d126f91ff7a5c8 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 28 Aug 2018 13:45:34 +0200 Subject: Make it possible to force all memory allocation functions to fail in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- vputenvf.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'vputenvf.c') diff --git a/vputenvf.c b/vputenvf.c index 53ddb30..ee823f2 100644 --- a/vputenvf.c +++ b/vputenvf.c @@ -7,23 +7,26 @@ int libsimple_vputenvf(const char *fmt, va_list ap) { va_list ap2; - int n; + int n, r; char *s, *p; va_copy(ap2, ap); n = vsnprintf(NULL, 0, fmt, ap2); va_end(ap2); if (n < 0) return -1; - s = alloca((size_t)n + 1); + s = malloc((size_t)n + 1); + if (!s) + return -1; vsprintf(s, fmt, ap); p = strchr(s, '='); if (p) { *p++ = '\0'; - return setenv(s, p, 1); + r = setenv(s, p, 1); + free(s); } else { - s = strdup(s); - return s ? putenv(s) : -1; + r = putenv(s); } + return r; } -- cgit v1.2.3-70-g09d2