接口中禁止使用原始数组¶
ID: cpp/array-in-interface
Kind: problem
Security severity:
Severity: recommendation
Precision: high
Tags:
- reliability
- readability
- language-features
- external/jsf
Query suites:
- cpp-security-and-quality.qls
此规则查找作为数组或使用数组的类成员(函数或数据)。这对具有数组类型参数的函数尤其重要,因为这些参数在函数内部被视为指向数组第一个元素的指针(数组衰减)。假设它仍然具有传递给函数的数组类型会导致意外行为(例如,使用 sizeof
运算符时)。
建议¶
使用 Array
类,或明确将变量/参数声明为指针,以避免混淆。
示例¶
void f(char buf[]) { //wrong: uses an array as a parameter type
int length = sizeof(buf); //will return sizeof(char*), not the size of the array passed
...
}
参考¶
AV 规则 97,《联合攻击战斗机空车 C++ 编码标准》。洛克希德·马丁公司,2005 年。