I found this paragraph in the man page for stdarg.h
:
Because the address of this parameter is used in the va_start() macro, it should not be declared as a register variable, or as a function or an array type.
So, register variable I understand, since a register can't be addressed with a pointer. Function I understand, since you would get the return value, which would use immediate addressing rather than address register indirect addressing.
I'm curious about what would happen if you used an array as the parameter. Say you use an array of three int
types. Would this result in the first element of the array being used as the last named parameter, while the next two elements would end up being used as the values for the variable arguments? This would be a buffer underrun.
I'm also wondering if this would result in a security vulnerability, e.g. someone could input elements of the array and have the function do something it wasn't supposed to do because it thinks the extra array elements are variable parameters.
Also, what about the printf
family of functions? Those use character arrays as their last named arguments. How do they not run into problems?