如何危险的是它访问数组越界? [英] How dangerous is it to access an array out of bounds?

查看:130
本文介绍了如何危险的是它访问数组越界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何危险的是访问(C语言)其边界以外的阵列?有时会发生,我从外面阵列读取(我现在明白了,然后我访问由我的程序的其他一些地方,甚至超出了使用的内存),或者我想一个值设置为一个索引数组之外。该程序有时会崩溃,但有时只是运行,只给意想不到的效果。

How dangerous is accessing an array outside of its bounds (in C)? It can sometimes happen that I read from outside the array (I now understand I then access memory used by some other parts of my program or even beyond that) or I am trying to set a value to an index outside of the array. The program sometimes crashes, but sometimes just runs, only giving unexpected results.

现在我想知道的是什么,如何危险的是这是真的吗?如果它损害我的程序,它并没有那么糟糕。如果它打破了我的计划之外的东西,因为我总算访问一些完全无关的记忆另一方面,则是非常糟糕的,我想。
我读了很多的任何事情都可能发生,<一个href=\"http://stackoverflow.com/questions/8313852/what-happens-if-my-pointer-crosses-the-array-bounds\">'segmentation可能是最不坏的问题,你的硬盘可能会变成粉红色和独角兽可能您的窗口,这是所有好的下唱歌,但什么是真正的危险?

Now what I would like to know is, how dangerous is this really? If it damages my program, it is not so bad. If on the other hand it breaks something outside my program, because I somehow managed to access some totally unrelated memory, then it is very bad, I imagine. I read a lot of 'anything can happen', 'segmentation might be the least bad problem', 'your harddisk might turn pink and unicorns might be singing under your window', which is all nice, but what is really the danger?

我的问题:


  1. 可以读取阵列之外的方式伤害值什么
    除了我的计划?我会想象只是看事情呢
    不会改变任何东西,或将它,例如改变'最后一次
    我碰巧文件的打开属性达到?

  2. 可以设置值出路阵外除了伤害我的东西
    程序?由此
        计算器问题我推测,有可能访问
        任何存储位置,没有任何安全保障。

  3. 我现在从X $ C $内c运行我的小程序。这是否
    提供围绕我的计划一些额外的保护它不能
    到达外自己的内存?它会伤害X code吗?

  4. 在如何安全地运行本质上我车code任何建议?

  1. Can reading values from way outside the array damage anything apart from my program? I would imagine just looking at things does not change anything, or would it for instance change the 'last time opened' attribute of a file I happened to reach?
  2. Can setting values way out outside of the array damage anything apart from my program? From this stackoverflow question I gather that it is possible to access any memory location, that there is no safety guarantee.
  3. I now run my small programs from within XCode. Does that provide some extra protection around my program where it cannot reach outside its own memory? Can it harm XCode?
  4. Any recommendations on how to run my inherently buggy code safely?

我用OSX 10.7,X code 4.6

I use OSX 10.7, Xcode 4.6

这是我的第一个问题#1。我花时间阅读,就像我可以在这个问题,但我可能错过了很多的资源。让我知道,如果你觉得我没有做足够的研究和/或看到其他问题这个问题。

This is my first Stackoverflow question. I took time reading as much as I could on the subject, but I probably missed many resources. Let me know if you feel I did not do enough research and/or you see other problems with this question.

推荐答案

至于ISO C标准(语言的官方定义)而言,访问它的边界之外的数组的未定义行为。这样做的字面意思是:

As far as the ISO C standard (the official definition of the language) is concerned, accessing an array outside its bounds has "undefined behavior". The literal meaning of this is:

行为,在使用不可移植的或错误的程序构造或
  错误数据,对于这本国际标准中并没有规定
  要求

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

一个非规范的音符扩展了这一点:

A non-normative note expands on this:

可能的不确定的行为是忽略的情况范围
  完全取消predictable成果,在翻译过程中的行为
  或执行程序中的一个记录方式的特点
  环境(有或没有发出一个诊断消息的),以
  终止翻译或执行(与发行的
  诊断消息)。

Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

这就是理论。什么是现实?

So that's the theory. What's the reality?

在最好的情况下,你会访问一些片的那要么你当前正在运行的程序拥有的(这可能会导致程序出现异常)的内存,或者说是的的由您目前拥有正在运行的程序(这可能会导致你的程序的东西,如分段故障崩溃)。或者你可以尝试写你的程序拥有的内存,但是这标记为只读;这大概也是导致程序崩溃。

In the "best" case, you'll access some piece of memory that's either owned by your currently running program (which might cause your program to misbehave), or that's not owned by your currently running program (which will probably cause your program to crash with something like a segmentation fault). Or you might attempt to write to memory that your program owns, but that's marked read-only; this will probably also cause your program to crash.

这是假设你的程序是一个操作系统,目的是保护并行运行的进程相互下运行。如果你的code是在裸机运行,说,如果它是一个操作系统内核或嵌入式系统的一部分,那么有没有这样的保护;你的行为不端code是什么应该提供这种保护。在这种情况下,损坏的可能性是相当大的,其中,在某些情况下,对硬件的物理损坏(或事物或人附近)。

That's assuming your program is running under an operating system that attempts to protect concurrently running processes from each other. If your code is running on the "bare metal", say if it's part of an OS kernel or an embedded system, then there is no such protection; your misbehaving code is what was supposed to provide that protection. In that case, the possibilities for damage are considerably greater, including, in some cases, physical damage to the hardware (or to things or people nearby).

即使在一个受保护的操作系统环境中,保护不总是100%。有操作系统漏洞,允许非特权的程序获得root(管理)访问,例如。即使普通用户的权限,一个恶意程序可能占用过多资源(CPU,内存,硬盘),可能会使整个系统。很多恶意软件(病毒等)的利用缓冲区溢出获得对系统的非法访问。

Even in a protected OS environment, the protections aren't always 100%. There are operating system bugs that permit unprivileged programs to obtain root (administrative) access, for example. Even with ordinary user privileges, a malfunctioning program can consume excessive resources (CPU, memory, disk), possibly bringing down the entire system. A lot of malware (viruses, etc.) exploits buffer overruns to gain unauthorized access to the system.

(一个历史的例子:我听说,在一些旧系统与核心显存,重复访问一个内存在紧凑循环中的位置可以从字面上引起的存储器融化该块。其他可能性包括破坏CRT显示器,以及移动与驱动机壳的谐波频率的磁盘驱动器的读/写头,导致它横跨表行走并落到地板上。)

(One historical example: I've heard that on some old systems with core memory, repeatedly accessing a single memory location in a tight loop could literally cause that chunk of memory to melt. Other possibilities include destroying a CRT display, and moving the read/write head of a disk drive with the harmonic frequency of the drive cabinet, causing it to walk across a table and fall onto the floor.)

和总有天网担心。

底线是:如果你可以写一个程序做坏事的有意的,这至少在理论上有可能是一个错误的程序可以做同样的事情的偶然

The bottom line is this: if you could write a program to do something bad deliberately, it's at least theoretically possible that a buggy program could do the same thing accidentally.

在实践中,它的非常的不太可能的MacOS X系统上运行你的车计划是要做什么比暴跌更加严重。但它不是可能的完全的prevent马车code从做非常糟糕的事情。

In practice, it's very unlikely that your buggy program running on a MacOS X system is going to do anything more serious than crash. But it's not possible to completely prevent buggy code from doing really bad things.

这篇关于如何危险的是它访问数组越界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆