diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-28 13:45:34 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-28 13:45:34 +0200 |
commit | f27708ce206f97896bcf001d00d126f91ff7a5c8 (patch) | |
tree | 622f2abc1f2d8535474c4f36709bb6dd40be39c1 /vputenvf.c | |
parent | Simplify en* functions, and handle argv0 == NULL correct (diff) | |
download | libsimple-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.c | 13 |
1 files changed, 8 insertions, 5 deletions
@@ -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; } |