任何程序都会在C/C++结构中检测到缓冲区溢出吗?

use*_*445 4 c c++ arrays struct bounds-checker

考虑以下程序:

struct abc
{
    int x[5];
    int y[5];
};

int main()
{
    struct abc test;
    test.y[0] = 10;
    printf("%d", test.x[5]);
}
Run Code Online (Sandbox Code Playgroud)

(借用来自结构的一个元素来查看另一个元素是否合法?)

BoundsChecker没有将此检测为溢出.是否有任何程序可以检测到这种类型的编程错误?

Car*_*rum 6

clang 是的,即使没有打开特殊标志:

$ clang example.c -o example
example.c:13:18: warning: array index of '5' indexes past the end of an array
      (that contains 5 elements) [-Warray-bounds]
    printf("%d", test.x[5]);
                 ^      ~
example.c:5:5: note: array 'x' declared here
    int x[5];
    ^
1 warning generated.
Run Code Online (Sandbox Code Playgroud)

编译为C++时会打印相同的警告.