☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

C 格式化字符串应为常量

10.6.8 ID_variableFormatString
目录 › next › previous

出于安全性和可读性的考量,C 格式化字符串最好直接写成常量字符串的形式。

本规则是 ID_hijack 的特化。

示例:

int a, b, c;
const char* fmt = foo();
....
printf(fmt, a, b, c);  // Non-compliant

例中格式化字符串 fmt 是变量,这种方式可读性较差,而且要注意如果 fmt 可受外界影响,则可能被攻击者利用造成不良后果。

应将 fmt 改为常量:

printf("%d %d %d", a, b, c);  // Compliant

相关

ID_hijack

参考

CWE-134
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.