缺少与转换说明符 'number' 对应的 'function-name' 的整数参数
此警告指示代码没有提供足够的参数来匹配格式字符串,缺失的参数中有一个是整数。
注解
这种缺陷可能会导致不正确的输出,在更危险的情况下,可能会导致堆栈溢出。
代码分析名称:MISSING_INTEGER_ARGUMENT_TO_FORMAT_FUNCTION
示例
以下代码生成此警告,因为调用 sprintf_s
时使用了数量不正确的参数,而缺少的参数是整数。 如果使用不安全函数 sprintf
而不是更安全的变体 sprintf_s
,则此代码可能会导致堆栈溢出,而不仅仅是意外的输出:
void f()
{
char buff[8];
const char *string="Hello";
sprintf_s(buff, sizeof(buff), "%s %d", string); // Attempts to print "Hello "
// followed by a number up to eleven characters long, depending on the garbage
// found on the stack. Any number other than a single non-negative digit can't
// fit in the 8 char buffer and leave room for the trailing null. If sprintf
// had been used instead, it would overflow.
}
若要更正此警告,请指定缺少的参数或调整格式字符串。 在此示例中,我们将添加缺失的整数值。
void f()
{
char buff[8];
const char *string = "Hello";
sprintf_s(buff, sizeof(buff), "%s %d", string, strlen(string));
}