...
Some template metaprogramming techniques that employ ""substitution failure is not an error" " (SFINAE) use variadic functions to implement compile-time type queries, as in:
| Code Block |
|---|
typedef char True;
typedef struct { char a[2]; } False;
template &lt;typename T&gt;<typename T>
True isPtr(T *);
False isPtr(...);
#define is_ptr(e) (sizeof(isPtr(e)) == sizeof(True))
|
...
| Code Block | ||
|---|---|---|
| ||
#include &lt;cstdarg&gt;<cstdarg> char *concatenate(char const *s, ...) { // code to actually concatenate the strings } char *separator = /* some reasonable value */; char *t = concatenate(&quot;hello&quot;"hello", separator, &quot;world&quot;"world", NULL); |
Calling this function without the trailing null pointer, or with an argument of any type other than ""pointer to possibly-CV-qualified char" " yields undefined behavior:
| Code Block | ||
|---|---|---|
| ||
char *u = concatenate(&quot;hello&quot;"hello", separator, &quot;world&quot;"world"); // undefined behavior char *v = concatenate(&quot;hello&quot;"hello", ' ', &quot;world&quot;"world", NULL); // undefined behavior |
...
| Code Block | ||
|---|---|---|
| ||
#include &lt;string&gt;<string> string separator = /* some reasonable value */; string s = &quot;hello&quot;"hello" + separator + &quot;world&quot;"world"; |
Risk Assessment
Incorrectly using a variadic function can result in abnormal program termination, unintended information disclosure, or execution of arbitrary code.
...