比如有测试代码:
int my_log(char* pFormat, ...)
{
char str[128] = {0};
va_list stArgList;
va_start (stArgList, pFormat);
alreadywrite = vsnprintf(str, 127, pFormat, stArgList);
va_end(stArgList);
printf("%s", str);
return 0;
}
int main()
{
unsigned long long input = 1234567890LL;
my_log("input is %d", input);
return 0;
}
在某些CPU平台中input值会输出异常,但是编译过程没有告警,如何处理呢?处理后的代码如下图所示,其中1,2表示pFormat是第几个参数:
int __attribute__((format(printf,1,2))) my_log(char* pFormat, ...)
{
char str[128] = {0};
va_list stArgList;
va_start (stArgList, pFormat);
alreadywrite = vsnprintf(str, 127, pFormat, stArgList);
va_end(stArgList);
printf("%s", str);
return 0;
}
int main()
{
unsigned long long input = 1234567890LL;
my_log("input is %d", input);
return 0;
}
上述代码编译就有告警了