Pra*_*ati 4 implementation programming-languages buffer-overflow
根据维基百科(http://en.wikipedia.org/wiki/Buffer_overflow)
通常与缓冲区溢出相关的编程语言包括C和C++,它们不提供内置保护以防止访问或覆盖内存的任何部分中的数据,也不会自动检查写入数组的数据(内置缓冲区类型)是否在该数组的边界.边界检查可以防止缓冲区溢出.
那么,为什么'Bounds Checking'没有在C和C++等语言中实现呢?
Cha*_*tin 10
基本上,这是因为它意味着每次更改索引时,都必须执行if语句.
让我们考虑一个简单的C for循环:
int ary[X] = {...}; // Purposefully leaving size and initializer unknown
for(int ix=0; ix< 23; ix++){
printf("ary[%d]=%d\n", ix, ary[ix]);
}
Run Code Online (Sandbox Code Playgroud)
如果我们有边界检查,生成的代码ary[ix]
必须是这样的
LOOP:
INC IX ; add `1 to ix
CMP IX, 23 ; while test
CMP IX, X ; compare IX and X
JGE ERROR ; if IX >= X jump to ERROR
LD R1, IX ; put the value of IX into register 1
LD R2, ARY+IX ; put the array value in R2
LA R3, Str42 ; STR42 is the format string
JSR PRINTF ; now we call the printf routine
J LOOP ; go back to the top of the loop
;;; somewhere else in the code
ERROR:
HCF ; halt and catch fire
Run Code Online (Sandbox Code Playgroud)
如果我们没有那个边界检查,那么我们可以改为:
LD R1, IX
LOOP:
CMP IX, 23
JGE END
LD R2, ARY+R1
JSR PRINTF
INC R1
J LOOP
Run Code Online (Sandbox Code Playgroud)
这样可以在循环中保存3-4条指令,这(特别是在过去)意味着很多.
事实上,在PDP-11机器中,它甚至更好,因为有一种称为"自动增量寻址"的东西.在PDP上,所有的寄存器等都变成了类似的东西
CZ -(IX), END ; compare IX to zero, then decrement; jump to END if zero
Run Code Online (Sandbox Code Playgroud)
(而且碰巧比我更好地记住PDP的人,不要给我带来精确语法等方面的麻烦;你是一个像我这样的老屁,你知道这些东西是如何溜走的.)
归档时间: |
|
查看次数: |
5650 次 |
最近记录: |