在C / C preventing缓冲区溢出++ [英] Preventing buffer overflow in C/C++
本文介绍了在C / C preventing缓冲区溢出++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很多时候,我有一个缓冲区溢出问题。
Many times I have problems with Buffer Overflow.
int y[10][10][10];
...
y[0][15][3] = 8;
我
如何prevent这个问题?
有没有什么好的工具,可以帮助我吗?
How can I prevent this problem? Is there any good tool that can help me?
推荐答案
尼尔的答案是在一般情况下比较好,但如果你有使用普通的旧数组的一个原因,你可以使用函数来获取和设置的值,也请确认您使用的数组边界内的:
Neil's answer is better in the general case, but if you have a reason for using plain old arrays, you can use functions to get and set the values and also check that you're within the array bounds:
#define MAX_INDEX 10
int y[MAX_INDEX][MAX_INDEX][MAX_INDEX];
int get_y(int a, int b, int c)
{
ASSERT(a >= 0 && a < MAX_INDEX);
ASSERT(b >= 0 && b < MAX_INDEX);
ASSERT(c >= 0 && c < MAX_INDEX);
return y[a][b][c];
}
void set_y(int a, int b, int c, int value)
{
ASSERT(a >= 0 && a < MAX_INDEX);
ASSERT(b >= 0 && b < MAX_INDEX);
ASSERT(c >= 0 && c < MAX_INDEX);
y[a][b][c] = value;
}
......都包裹在一个类中,理想的。
...all wrapped up in a class, ideally.
这篇关于在C / C preventing缓冲区溢出++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文