ORCA-C/Tests/Conformance/c99stdarg.c
Stephen Heumann ed3035cb99 Fix bug in code for varargs functions with multiple fixed parameters.
This was broken by the varargs changes in commit a20d69a211. The code was not accounting for the internal representation of the parameters being in reverse order, so it was basing address calculations on the first fixed parameter rather than the last one, resulting in the wrong number of bytes being removed from the stack (generally causing a crash).

This affected the c99stdio.c test case, and is now also covered in c99stdarg.c.
2022-01-01 22:42:42 -06:00

72 lines
1.5 KiB
C

/*
* Test <stdarg.h> functionality, including va_copy (from C99).
*/
#include <stdarg.h>
#include <stdio.h>
int va_list_fn(va_list ap) {
va_list ap2;
/* Test use of va_copy in a function that was passed a va_list */
va_copy(ap2, ap);
if (va_arg(ap2, double) != 67890.0)
return 0;
if (va_arg(ap2, long) != 1234567890)
return 0;
va_end(ap2);
return 1;
}
int va_fn(int x, int y, int z, ...) {
va_list ap, ap2;
int i, *ip = &i;
/* Test basic varargs functionality */
va_start(ap, z);
if (va_arg(ap, int) != 12345)
return 0;
/* Test va_copy */
va_copy(ap2, ap);
if (va_arg(ap2, double) != 67890.0)
return 0;
/* Test passing a va_list to another function */
if (!va_list_fn(ap))
return 0;
va_end(ap);
va_end(ap2);
/* Test that varargs processing can be restarted */
va_start(ap, z);
if (va_arg(ap, int) != 12345)
return 0;
va_end(ap);
/* Test that va_end does not change local variable addresses */
if (&i != ip)
return 0;
return 1;
}
int main(void) {
if (!va_fn(1, 2, 3, 12345, 67890.0, 1234567890L))
goto Fail;
printf ("Passed Conformance Test c99stdarg\n");
return 0;
Fail:
printf ("Failed Conformance Test c99stdarg\n");
}