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 --- Makefile | 2 +- envputenvf.c | 2 +- libsimple.h | 18 +++++++++--------- test.c | 25 +++++++++++++++++++++++++ vputenvf.c | 13 ++++++++----- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 505ab47..3c20ae3 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ libsimple.a: $(OBJ) $(AR) -s $@ .test.o.test: - $(CC) -o $@ $< libsimple.a test.o $(LDFLAGS) + $(CC) -o $@ $< test.o libsimple.a $(LDFLAGS) .c.test.o: $(CC) -c -o $@ $< $(CFLAGS) -DTEST diff --git a/envputenvf.c b/envputenvf.c index cdcf4f5..7bc1f0a 100644 --- a/envputenvf.c +++ b/envputenvf.c @@ -6,7 +6,7 @@ void libsimple_envputenvf(int status, const char *fmt, va_list ap) /* TODO test */ { - if (vputenvf(fmt, ap)) + if (libsimple_vputenvf(fmt, ap)) enprintf(status, "putenvf:"); } diff --git a/libsimple.h b/libsimple.h index 6dfd62b..7f54b8a 100644 --- a/libsimple.h +++ b/libsimple.h @@ -1228,7 +1228,7 @@ libsimple_putenvf(const char *__fmt, ...) { va_list __ap; va_start(__ap, __fmt); - return vputenvf(__fmt, __ap); + return libsimple_vputenvf(__fmt, __ap); va_end(__ap); } #ifndef putenvf @@ -1241,7 +1241,7 @@ libsimple_eputenvf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); - envputenvf(libsimple_default_failure_exit, __fmt, __ap); + libsimple_envputenvf(libsimple_default_failure_exit, __fmt, __ap); va_end(__ap); } #ifndef eputenvf @@ -1252,7 +1252,7 @@ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline void libsimple_evputenvf(const char *__fmt, va_list __ap) /* TODO test */ { - envputenvf(libsimple_default_failure_exit, __fmt, __ap); + libsimple_envputenvf(libsimple_default_failure_exit, __fmt, __ap); } #ifndef evputenvf # define evputenvf libsimple_evputenvf @@ -1264,7 +1264,7 @@ libsimple_enputenvf(int __status, const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); - envputenvf(__status, __fmt, __ap); + libsimple_envputenvf(__status, __fmt, __ap); va_end(__ap); } #ifndef enputenvf @@ -1284,7 +1284,7 @@ libsimple_eprintf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); - vweprintf(__fmt, __ap); + libsimple_vweprintf(__fmt, __ap); va_end(__ap); exit(libsimple_default_failure_exit); } @@ -1296,7 +1296,7 @@ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(1), __noreturn__))) static inline void libsimple_veprintf(const char *__fmt, va_list __ap) /* TODO test */ { - vweprintf(__fmt, __ap); + libsimple_vweprintf(__fmt, __ap); exit(libsimple_default_failure_exit); } #ifndef veprintf @@ -1309,7 +1309,7 @@ libsimple_enprintf(int __status, const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); - vweprintf(__fmt, __ap); + libsimple_vweprintf(__fmt, __ap); va_end(__ap); exit(__status); } @@ -1321,7 +1321,7 @@ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__(2), __noreturn__))) static inline void libsimple_venprintf(int __status, const char *__fmt, va_list __ap) /* TODO test */ { - vweprintf(__fmt, __ap); + libsimple_vweprintf(__fmt, __ap); exit(__status); } #ifndef venprintf @@ -1334,7 +1334,7 @@ libsimple_weprintf(const char *__fmt, ...) /* TODO test */ { va_list __ap; va_start(__ap, __fmt); - vweprintf(__fmt, __ap); + libsimple_vweprintf(__fmt, __ap); va_end(__ap); } #ifndef weprintf diff --git a/test.c b/test.c index 05d74d4..ad5a4bc 100644 --- a/test.c +++ b/test.c @@ -3,6 +3,9 @@ #include "test.h" #include +#undef strndup +#undef memdup + size_t alloc_fail_in = 0; @@ -185,6 +188,28 @@ pvalloc(size_t size) } +char * +strdup(const char *s) +{ + char *r = malloc(strlen(s) + 1); + return r ? strcpy(r, s) : r; +} + + +char * +strndup(const char *s, size_t size) +{ + return libsimple_strndup(s, size); +} + + +void * +memdup(const void *s, size_t size) +{ + return libsimple_memdup(s, size); +} + + void free(void *ptr) { 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