aboutsummaryrefslogtreecommitdiffstats
path: root/vputenvf.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-08-28 13:45:34 +0200
committerMattias Andrée <maandree@kth.se>2018-08-28 13:45:34 +0200
commitf27708ce206f97896bcf001d00d126f91ff7a5c8 (patch)
tree622f2abc1f2d8535474c4f36709bb6dd40be39c1 /vputenvf.c
parentSimplify en* functions, and handle argv0 == NULL correct (diff)
downloadlibsimple-f27708ce206f97896bcf001d00d126f91ff7a5c8.tar.gz
libsimple-f27708ce206f97896bcf001d00d126f91ff7a5c8.tar.bz2
libsimple-f27708ce206f97896bcf001d00d126f91ff7a5c8.tar.xz
Make it possible to force all memory allocation functions to fail in tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--vputenvf.c13
1 files changed, 8 insertions, 5 deletions
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;
}