热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

防止C/C++中的缓冲区溢出-PreventingbufferoverflowinC/C++

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 个解决方案

#1


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:

在一般情况下,Neil的答案更好,但是如果你有理由使用普通旧数组,你可以使用函数来获取和设置值,并检查你是否在数组边界内:

#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 = 0 && b = 0 && c = 0 && a = 0 && b = 0 && c 

...all wrapped up in a class, ideally.

......理想情况下,所有人都在课堂上。

#2


Don't use raw C-style arrays. Instead, use C++ container classes such as std::vector, which have the ability to check for invalid accesses and raise exceptions when they occur.

不要使用原始C风格的数组。相反,使用诸如std :: vector之类的C ++容器类,它们能够检查无效访问并在异常访问发生时引发异常。

Also, what you are describing is not really a buffer overflow.

此外,您所描述的并不是真正的缓冲区溢出。

#3


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忽略,因为变量在堆栈上,而不在堆上)。

#4


In addition to the other comments, you might also have a look at the suggestions in this thread, which deals with static code analysis tools:

除了其他注释之外,您还可以查看此主题中的建议,该主题涉及静态代码分析工具:

C/C++ Free alternative to Lint?

C / C ++免费替代Lint?

#5


I've found an interesting software for buffer overflow. You can download it for free from www.bugfighter-soft.com

我发现了一个有趣的缓冲区溢出软件。您可以从www.bugfighter-soft.com免费下载

It says that it can discover buffer overflow and that it is independent from compiler and platform.

它说它可以发现缓冲区溢出,并且它独立于编译器和平台。

I tried it with Visual C++ Express 2008 and it worked well. I could discover buffer overflow in a multidimensional array such int y[10][10][10];

我尝试使用Visual C ++ Express 2008,它运行良好。我可以发现多维数组中的缓冲区溢出,例如[10] [10] [10];

Do you think it is cross platform?

你认为它是跨平台的吗?

Do you know something more about it?

你知道更多关于它的事吗?

#6


using sprintf in TRACE MACROS is the biggest evil

在TRACE MACROS中使用sprintf是最大的罪恶


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • LeetCode笔记:剑指Offer 41. 数据流中的中位数(Java、堆、优先队列、知识点)
    本文介绍了LeetCode剑指Offer 41题的解题思路和代码实现,主要涉及了Java中的优先队列和堆排序的知识点。优先队列是Queue接口的实现,可以对其中的元素进行排序,采用小顶堆的方式进行排序。本文还介绍了Java中queue的offer、poll、add、remove、element、peek等方法的区别和用法。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • JVM:33 如何查看JVM的Full GC日志
    1.示例代码packagecom.webcode;publicclassDemo4{publicstaticvoidmain(String[]args){byte[]arr ... [详细]
  • 初识java关于JDK、JRE、JVM 了解一下 ... [详细]
  • 201720181 20155339 《信息安全系统设计基础》第六周学习总结
    2017-2018-120155339《信息安全系统设计基础》第六周学习总结教材学习内容总结控制转移:从ak指令到a(k1)指令的过渡。控制转移序列称为处理器的控制流 ... [详细]
  • Apple iPad:过渡设备还是平板电脑?
    I’vebeenagonizingoverwhethertopostaniPadarticle.Applecertainlydon’tneedmorepublicityandthe ... [详细]
  • 开发笔记:快速排序和堆排序
    本文由编程笔记#小编为大家整理,主要介绍了快速排序和堆排序相关的知识,希望对你有一定的参考价值。快速排序思想:在partition中,首先以最右边的值作为划分值x,分别维护小于 ... [详细]
  • 尾部|柜台_Java并发线程池篇附场景分析
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java并发-线程池篇-附场景分析相关的知识,希望对你有一定的参考价值。作者:汤圆个人博客 ... [详细]
  • 4.3.2Tuple是否可以跨页面PostgreSQLusesafixedpagesize(commonly8kB),anddoesnotallowtuplestospanmult ... [详细]
author-avatar
哈王豐3_408
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有