1.struct.c的代码展示
2.运行实例
3.详细解释及知识点
1.strruct.c的代码如下
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a[2];
double d;
} struct_t;
double fun(int i) {
volatile struct_t s;
s.d = 3.14;
s.a[i] = 1073741824; /* Possibly out of bounds */
return s.d; /* Should be 3.14 */
}
int main(int argc, char *argv[]) {
int i = 0;
if (argc >= 2)
i = atoi(argv[1]);
double d = fun(i);
printf("fun(%d) --> %.10f\n", i, d);
return 0;
}
2.运行实例
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 0
fun(0) --> 3.1400000000
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 1
fun(1) --> 3.1400000000
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 2
fun(2) --> 3.1399998665
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 3
fun(3) --> 2.0000006104
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 4
fun(4) --> 3.1400000000
段错误 (核心已转储)
3.详细解释
为什么当i>1时就会有问题?
在结构体的存储区由低地址到高地址依次存放int型a[0],a[1],double型的d,当i>1时,1073741824会覆盖掉d中的数据。由于d的存储区由低到高是按尾数,阶码,符号的顺序存放,所以i=2时没有i=3时覆盖的数据造成的影响严重。
知识点
越界访问和缓冲区溢出
C语言程序中对数组的访问可能会有意或无意地超越数组存储区范围
而无法发现。
数组存储区可看成是一个缓冲区,超越数组存储区范围的写入操作
称为缓冲区溢出。
–例如,对于一个有10个元素的char型数组,其定义的缓冲区有10个字
节。若写一个字符串到这个缓冲区,那么只要写入的字符串多于9个字
符(结束符’\0’占一个字节) ,就会发生“写溢出“ 。
缓冲区溢出是一种非常普遍、非常危险的漏洞,在各种操作系统、
应用软件中广泛存在。
缓冲区溢出攻击是利用缓冲区溢出漏洞所进行的攻击。利用缓冲区
溢出攻击,可导致程序运行失败、系统关机、重新启动等后果。