如何在 gcc / mingw 中进行边界检查?
尝试过这个
int main(void){
int a[10]; a[20]=5;
}
-Wall -O2 main.c
它没有给我任何警告...
它是Windows(mingw)内的gcc,我无法检测到这种边界限制错误
如何告诉编译器检查它? mingw可以做到吗?
谢谢
Having tried this
int main(void) {
int a[10];
a[20]=5;}
gcc -Wall -O2 main.c
It gives me no warning...
It's gcc within windows (mingw) and I am not able to detect this kind of boundary limit bug
how to tell compiler to check it? can mingw do it?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有人尝试处理数组边界检查。默认情况下,santdard C99 没有提及强制数组边界,我相信很大程度上是因为它有更多的开销。
话虽这么说,您可以查看这样的网站,人们试图处理它:
http://williambader .com/bounds/example.html
There are attempts to deal with array bounds checking. By default, the santdard C99 says nothing about enforcing array bounds, I believe largely because it has more overhead.
That being said you can look at sites like this where people have tried to deal with it:
http://williambader.com/bounds/example.html
还有其他非编译工具可以使用静态分析来查找数组边界违规等错误。之前的SO问题讨论了其中的一些问题。请注意,如果您需要在可能限制您选择的 mingw 环境中运行。
There are other non-compiler tools that can use static analysis to find errors like array boundary violations. A previous SO question discusses some of them. Mind you, if you're needing to run in a mingw environment that may limit your choices.
这个问题很老了,但我想我在这里得到了另一个答案。即使您计划在 gcc 上进行完整构建,您也可以尝试 clang 编译器(例如,对于大型项目中的夜间构建)。效果很好。
一个例子是当我尝试访问索引 4 时的输出,其中数组大小声明为 4:
谢谢
卡贾尔
The question is quite old but I think another answer I got here. Even though if you plan to do a full build on gcc, you can give a try to clang compiler (e.g. for nightly builds in large projects). It worked quite well.
An example is the output when I tried accessing index 4 where the array size is declared as 4:
Thanks
Kajal