静态数组访问可能导致溢出¶
ID: cpp/static-buffer-overflow
Kind: problem
Security severity: 9.3
Severity: warning
Precision: high
Tags:
- reliability
- security
- external/cwe/cwe-119
- external/cwe/cwe-131
Query suites:
- cpp-code-scanning.qls
- cpp-security-extended.qls
- cpp-security-and-quality.qls
使用静态数组时,必须确保在写入和访问操作期间不会超出数组的大小。如果操作尝试写入或访问数组范围之外的元素,则会导致缓冲区溢出。缓冲区溢出可能导致从段错误到安全漏洞的各种问题。
建议¶
检查高亮显示的操作中使用的偏移量和大小,以确保不会发生缓冲区溢出。
示例¶
#define SIZE 30
int f(char * s) {
char buf[20]; //buf not set to use SIZE macro
strncpy(buf, s, SIZE); //wrong: copy may exceed size of buf
for (int i = 0; i < SIZE; i++) { //wrong: upper limit that is higher than array size
cout << array[i];
}
}