linux mmap出现Address 0x7ffff7ff6000 out of bounds>

来源:互联网 发布:爱奇艺 网络环境危险 编辑:程序博客网 时间:2024/06/06 01:45
 **mmap.c**  1 #include <stdio.h>  2 #include <fcntl.h>  3 #include <signal.h>  4 #include <sys/mman.h>  5 #include <unistd.h>  6 #define FILESIZE 8192  7   8 int main()  9 { 10     int i; 11     char *p, tmp; 12     struct stat sb; 13     int fd = open("1.txt", O_RDWR | O_CREAT, S_IRUSR|S_IWUSR); 14     p = (char*)mmap(NULL,FILESIZE, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0); 15     for (i=0; i<10; i++) { 16         p[i] = 'a'; 17     } 18     close(fd); 19     munmap(p, FILESIZE); 20     printf("ok\n"); 21     return 0; 22 }

gcc mmap.c生成执行程序a.out, 运行a.out会出现总线错误 (核心已转储)

(gdb) p p $2 = 0x7ffff7ff6000 <Address 0x7ffff7ff6000 out of bounds>

用gdb去调试它会发现line 14映射的地址p有问题,而导致p有问题的原因是因为映射的文件1.txt的大小为0,也就是个空文件,修改方法很简单,让1.txt大小不为0就可以了。

mmap函数原型介绍

0 1
原创粉丝点击