作者:哈王豐3_408 | 来源:互联网 | 2023-09-13 06:45
ManytimesIhaveproblemswithBufferOverflow.很多次我遇到BufferOverflow问题。inty[10][10][10];
Many times I have problems with Buffer Overflow.
很多次我遇到Buffer Overflow问题。
int y[10][10][10];
...
y[0][15][3] = 8;
How can I prevent this problem? Is there any good tool that can help me?
我该如何防止这个问题?有什么好的工具可以帮助我吗?
6 个解决方案
Solution at the code level
In C++, one solution is to never use arrays, but C++ containers instead. Vectors, for example, have out of bounds detection if you use at intead of [] for indexing
在C ++中,一种解决方案是永远不使用数组,而是使用C ++容器。例如,如果在[]的intead处使用索引,则向量具有超出范围的检测
In C, you should always design your functions such as you give the pointers and the dimension(s) of your arrays, there is no way around it.
在C中,你应该总是设计你的函数,比如你给出指针和数组的维度,没有办法解决它。
Solution at the tool level
A great tool for checking out of bounds access is valgrind. It works by running your binary unaltered, and can give the precise line where errors occurs if you compile with debug information. Valgrind work on many unix, including mac os x.
valgrind是一个检查越界访问的好工具。它的工作方式是不加改变地运行二进制文件,如果使用调试信息进行编译,则可以给出出现错误的精确行。 Valgrind在许多unix上工作,包括mac os x。
Note that valgrind cannot always detect those bad accesses (in your example, assuming it was a real out of bounds access, it would have gonve unnoticed by valgrind because the variable is on the stack, not on the heap).
请注意,valgrind无法始终检测到那些错误的访问(在您的示例中,假设它是一个真正的越界访问,它会被valgrind忽略,因为变量在堆栈上,而不在堆上)。