From 5382c3cc9b24f59a050b4ef862cab392fa33ed30 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sat, 2 Feb 2019 16:05:12 +0100 Subject: Test for NULL in variable argument lists Sometimes a vararg function receives a NULL-terminated list of pointers. This can fail if sizeof(NULL) < sizeof(void *), as this test illustrates. --- test/regression/Makefile | 2 +- test/regression/varargs3.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/regression/varargs3.c (limited to 'test') diff --git a/test/regression/Makefile b/test/regression/Makefile index 191a2285..5d40e2c9 100644 --- a/test/regression/Makefile +++ b/test/regression/Makefile @@ -23,7 +23,7 @@ TESTS=int32 int64 floats floats-basics \ TESTS_COMP=attribs1 bitfields1 bitfields2 bitfields3 bitfields4 \ bitfields5 bitfields6 bitfields7 bitfields8 \ builtins-$(ARCH) packedstruct1 packedstruct2 alignas \ - varargs1 varargs2 sections alias + varargs1 varargs2 varargs3 sections alias # Can run, both in compiled mode and in interpreter mode, # but produce processor-dependent results, so no reference output in Results diff --git a/test/regression/varargs3.c b/test/regression/varargs3.c new file mode 100644 index 00000000..a46d81e3 --- /dev/null +++ b/test/regression/varargs3.c @@ -0,0 +1,52 @@ +#include +#include + +void initialize(int first, ...) +{ + va_list ap; + va_start(ap, first); + while (1) { + int * p = va_arg(ap, int *); + if (p == NULL) break; + *p = first; + first++; + } +} + +void test(void) +{ + int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q; + initialize(42, &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, + &k, &l, &m, &n, &o, &p, &q, NULL); + printf("a = %d\n", a); + printf("b = %d\n", b); + printf("c = %d\n", c); + printf("d = %d\n", d); + printf("e = %d\n", e); + printf("f = %d\n", f); + printf("g = %d\n", g); + printf("h = %d\n", h); + printf("i = %d\n", i); + printf("j = %d\n", j); + printf("k = %d\n", k); + printf("l = %d\n", l); + printf("m = %d\n", m); + printf("n = %d\n", n); + printf("o = %d\n", o); + printf("p = %d\n", p); + printf("q = %d\n", q); +} + +void wipestack(void) +{ + unsigned int b[100]; + int i; + for (i = 0; i < 100; i++) ((volatile unsigned int *)b)[i] = 0xDEADBEEFU; +} + +int main() +{ + wipestack(); + test(); + return 0; +} -- cgit