如何在 gcc / mingw 中进行边界检查?

发布于 2024-09-05 06:47:06 字数 251 浏览 7 评论 0原文

尝试过这个

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟雨扶苏 2024-09-12 06:47:06

有人尝试处理数组边界检查。默认情况下,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

無心 2024-09-12 06:47:06

还有其他非编译工具可以使用静态分析来查找数组边界违规等错误。之前的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.

美煞众生 2024-09-12 06:47:06

这个问题很老了,但我想我在这里得到了另一个答案。即使您计划在 gcc 上进行完整构建,您也可以尝试 clang 编译器(例如,对于大型项目中的夜间构建)。效果很好。

一个例子是当我尝试访问索引 4 时的输出,其中数组大小声明为 4:

    ex9.c:17:2: warning: array index 4 is past the end of the array (which contains
          4 elements) [-Warray-bounds]
            numbers[4] = 4;
            ^       ~
    ex9.c:4:2: note: array 'numbers' declared here
            int numbers[4] = {0};
            ^

谢谢
卡贾尔

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:

    ex9.c:17:2: warning: array index 4 is past the end of the array (which contains
          4 elements) [-Warray-bounds]
            numbers[4] = 4;
            ^       ~
    ex9.c:4:2: note: array 'numbers' declared here
            int numbers[4] = {0};
            ^

Thanks
Kajal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文