...
In this example, is_ptr(e) returns true if expression e has a pointer type. is_ptr(e) calls variadic function isPtr(...), but the call takes place in a sizeof expression. Consequently, isPtr(...) must be declared, but it need not, and should not, be defined.
Non-compliant Code Example
This example uses a variadic function to concatenate an arbitrary number of null-terminated character sequences (NTCS) in a single NTCS. Each call to the function must use a null pointer value to mark the end of the argument list.
...
| Code Block | ||
|---|---|---|
| ||
char *u = concatenate("hello", separator, "world"); // undefined behavior
char *v = concatenate("hello", ' ', "world", NULL); // undefined behavior
|
Compliant Solution
Rather than use a variadic function, you can use a chain of binary operations:
| Code Block | ||
|---|---|---|
| ||
#include <string> string separator = /* some reasonable value */; string s = "hello" + separator + "world"; |
Risk Assessment
Incorrectly using a variadic function can result in abnormal program termination, unintended information disclosure, or execution of arbitrary code.
...