c ++编译时数组边界检查不完全在clang中工作

时间:2018-01-20 14:34:01

标签: c++ arrays clang static-analysis clang-static-analyzer

我想检查编译时数组绑定违规,特别是std::array。不幸的是,此检查仅适用于裸标准数组,不适用于任何类型的包装数组。

#include <array>
#include <iostream>

struct Array{
    int data[2];

    int operator[](size_t i){
        return data[i];    
    }
};

int main() {
  int test1[]{1,2};
  std::cout << test1[2] << std::endl;

  std::array<int,2> test2{1,2};
  std::cout << test2[2] << std::endl;

  Array test3;
  std::cout << test3[2] << std::endl;
}

使用clang时的输出:

main.cpp:15:16: warning: Access out-of-bound array element (buffer overflow)

  std::cout << test1[2] << std::endl;

               ^~~~~~~~

main.cpp:15:16: note: Access out-of-bound array element (buffer overflow)

  std::cout << test1[2] << std::endl;

               ^~~~~~~~

1 warning generated.

编译选项是: clang++ -std=c++11 -stdlib=libc++ main.cpp -Weverything -Warray-bounds -Reverything --analyze -Xanalyzer -analyzer-output=text -Xanalyzer -analyzer-checker=alpha.security

见coliru:http://coliru.stacked-crooked.com/a/c21f371f142ba51d

有没有办法检查这些错误?

0 个答案:

没有答案