CodeQL 文档

块中声明的函数

ID: cpp/function-in-block
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - maintainability
   - readability
   - external/jsf
Query suites:
   - cpp-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

此规则查找在代码块中声明的函数。在块作用域中声明函数会造成混淆,函数的可见性也不符合预期。在代码块内部声明的 extern 函数声明(在 C 中允许)尤其令人困惑,因为声明的作用域是整个文件,而不是声明所在的代码块。

建议

在文件作用域中声明函数,以避免对其可见性的任何混淆。

示例

int f() {
	extern int other(); //scope of externs is the entire file, not just the 
	                    //block where it is declared
	...
	other()
}

int g() {
	other(); //this will use the other() function declared inside f()
}

参考资料

  • AV 规则 107,联合攻击战斗机空中载具 C++ 编码标准。洛克希德·马丁公司,2005 年。

  • MISRA C++ 规则 3-1-2,关键系统中 C++ 语言使用指南。汽车工业软件可靠性协会,2008 年。

  • ©GitHub, Inc.
  • 条款
  • 隐私