未终止的可变参数调用¶
ID: cpp/unterminated-variadic-call
Kind: problem
Security severity: 8.8
Severity: warning
Precision: medium
Tags:
- reliability
- security
- external/cwe/cwe-121
Query suites:
- cpp-security-extended.qls
- cpp-security-and-quality.qls
程序调用了一个函数,该函数期望使用哨兵值(通常为 NULL、0 或 -1)终止可变参数列表。在这种情况下,哨兵值已被省略作为最终参数。此缺陷可能会导致函数行为不正确以及意外的堆栈内存访问,从而导致程序结果错误、不稳定,甚至容易受到缓冲区溢出式攻击。
建议¶
此规则突出显示的每个缺陷描述都包含一个建议的终止符值。检查此值是否正确,然后将其添加到调用的末尾。
示例¶
#include <stdarg.h>
void pushStrings(char *firstString, ...)
{
va_list args;
char *arg;
va_start(args, firstString);
// process inputs, beginning with firstString, ending when NULL is reached
arg = firstString;
while (arg != NULL)
{
// push the string
pushString(arg);
// move on to the next input
arg = va_arg(args, char *);
}
va_end(args);
}
void badFunction()
{
pushStrings("hello", "world", NULL); // OK
pushStrings("apple", "pear", "banana", NULL); // OK
pushStrings("car", "bus", "train"); // BAD, not terminated with the expected NULL
}
在此示例中,对 pushStrings
的第三次调用未正确终止。此调用应更新为包括 NULL
作为此调用的第四个也是最后一个参数。
参考¶
常见弱点枚举:CWE-121。