diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-29 17:51:58 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-29 17:51:58 +0200 |
commit | 951b4d882fa2393369656e05efef3bb229e38c1d (patch) | |
tree | 1630753309c5e0d993345d7a4a41cf45dab1cde8 | |
parent | Add readme (diff) | |
download | libsimple-951b4d882fa2393369656e05efef3bb229e38c1d.tar.gz libsimple-951b4d882fa2393369656e05efef3bb229e38c1d.tar.bz2 libsimple-951b4d882fa2393369656e05efef3bb229e38c1d.tar.xz |
Circumvent GCC bugs in test code when optimised
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | config.mk | 4 | ||||
-rw-r--r-- | libsimple.c | 2 | ||||
-rw-r--r-- | test.c | 34 | ||||
-rw-r--r-- | test.h | 18 |
4 files changed, 31 insertions, 27 deletions
@@ -1,3 +1,3 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -CFLAGS = -std=c99 -Wall -Wextra -g $(CPPFLAGS) -LDFLAGS = +CFLAGS = -std=c99 -Wall -Wextra -O2 $(CPPFLAGS) +LDFLAGS = -s diff --git a/libsimple.c b/libsimple.c index 58565b5..d6eb2ce 100644 --- a/libsimple.c +++ b/libsimple.c @@ -90,7 +90,7 @@ test_vasprintfa(const char *expected, const char *format, ...) int main(void) { - struct allocinfo *info; + struct allocinfo *volatile info; void *ptr, *ptr2; struct timespec ts, ts1, ts2; struct timeval tv1, tv2; @@ -10,17 +10,17 @@ char *argv0 = (char []){"<test>"}; -size_t alloc_fail_in = 0; -int exit_real = 0; -int exit_ok = 0; -int exit_status; +volatile size_t alloc_fail_in = 0; +volatile int exit_real = 0; +volatile int exit_ok = 0; +volatile int exit_status; jmp_buf exit_jmp; -char stderr_buf[8 << 10]; -size_t stderr_n = 0; -int stderr_real = 0; -int stderr_ok = 0; +volatile char stderr_buf[8 << 10]; +volatile size_t stderr_n = 0; +volatile int stderr_real = 0; +volatile int stderr_ok = 0; -static int custom_malloc = 0; +static volatile int custom_malloc = 0; size_t @@ -72,7 +72,7 @@ void * calloc(size_t nelem, size_t elsize) { struct allocinfo *info; - void *ret; + void *volatile ret; assert(nelem && elsize); /* unspecified behaviour otherwise */ if (nelem > SIZE_MAX / elsize) { errno = ENOMEM; @@ -92,7 +92,7 @@ void * realloc(void *ptr, size_t size) { struct allocinfo *info; - void *ret; + void *volatile ret; size_t n; assert(size); /* unspecified behaviour otherwise */ if (!ptr) @@ -113,7 +113,7 @@ void * memalign(size_t alignment, size_t size) { struct allocinfo *info; - void *ptr; + void *volatile ptr; size_t n; uintptr_t off; @@ -166,7 +166,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) { int ret, saved_errno = errno; - void **volatile ptrp = memptr; + void *volatile *volatile ptrp = memptr; assert(!(alignment % sizeof(void *))); assert(ptrp); *memptr = memalign(alignment, size); @@ -264,6 +264,7 @@ fprintf(FILE *restrict stream, const char *restrict format, ...) int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) { + size_t old_alloc_fail_in = alloc_fail_in; va_list ap2; int r; char *buf; @@ -275,17 +276,20 @@ vfprintf(FILE *restrict stream, const char *restrict format, va_list ap) if (r >= 0) { n = (size_t)r; - buf = alloca(n + 1); + alloc_fail_in = 0; + assert((buf = malloc(n + 1))); n = vsnprintf(buf, n + 1, format, ap); if (fileno(stream) != STDERR_FILENO || stderr_real) { fwrite(buf, 1, n, stream); } else { assert(stderr_ok); assert(stderr_n + n <= sizeof(stderr_buf)); - memcpy(&stderr_buf[stderr_n], buf, n); + memcpy((char *)(void *)(&stderr_buf[stderr_n]), buf, n); stderr_n += n; } + free(buf); } + alloc_fail_in = old_alloc_fail_in; return r; } @@ -51,7 +51,7 @@ len__ = sprintf(buf__, FMT, __VA_ARGS__);\ assert(len__ >= 0);\ assert((size_t)len__ == stderr_n);\ - assert(!memcmp(buf__, stderr_buf, stderr_n));\ + assert(!memcmp(buf__, (char **)(void *)(&stderr_buf), stderr_n)); \ } while (0); @@ -68,15 +68,15 @@ struct allocinfo { extern char *argv0; -extern size_t alloc_fail_in; -extern int exit_real; -extern int exit_ok; -extern int exit_status; +extern volatile size_t alloc_fail_in; +extern volatile int exit_real; +extern volatile int exit_ok; +extern volatile int exit_status; extern jmp_buf exit_jmp; -extern char stderr_buf[8 << 10]; -extern size_t stderr_n; -extern int stderr_real; -extern int stderr_ok; +extern volatile char stderr_buf[8 << 10]; +extern volatile size_t stderr_n; +extern volatile int stderr_real; +extern volatile int stderr_ok; size_t get_pagesize(void); |