闲言不多说,先上代码:
- #include
- #include
- int free_inside(int * p)
- {
- if(p)
- free(p);
- }
- int main()
- {
- int *p = malloc(100*sizeof(int));
- free_inside(p);
- free(p);
- return 0;
- }
- root@libin:~/program/C/mem_bug# env MALLOC_CHECK_=1 ./test
- *** glibc detected *** ./test: free(): invalid pointer: 0x08d22008 ***
- root@libin:~/program/C/mem_bug# ./test
- *** glibc detected *** ./test: double free or corruption (top): 0x0943e008 ***
- ======= Backtrace: =========
- /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xba4591]
- /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0xba5de8]
- /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xba8ecd]
- ./test[0x804845e]
- /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb4fbd6]
- ./test[0x8048381]
- ======= Memory map: ========
- 00526000-00541000 r-xp 00000000 08:0a 532971 /lib/ld-2.11.1.so
- 00541000-00542000 r--p 0001a000 08:0a 532971 /lib/ld-2.11.1.so
- 00542000-00543000 rw-p 0001b000 08:0a 532971 /lib/ld-2.11.1.so
- 0060e000-0060f000 r-xp 00000000 00:00 0 [vdso]
- 00b39000-00c8c000 r-xp 00000000 08:0a 534966 /lib/tls/i686/cmov/libc-2.11.1.so
- 00c8c000-00c8d000 ---p 00153000 08:0a 534966 /lib/tls/i686/cmov/libc-2.11.1.so
- 00c8d000-00c8f000 r--p 00153000 08:0a 534966 /lib/tls/i686/cmov/libc-2.11.1.so
- 00c8f000-00c90000 rw-p 00155000 08:0a 534966 /lib/tls/i686/cmov/libc-2.11.1.so
- 00c90000-00c93000 rw-p 00000000 00:00 0
- 00eed000-00f0a000 r-xp 00000000 08:0a 524925 /lib/libgcc_s.so.1
- 00f0a000-00f0b000 r--p 0001c000 08:0a 524925 /lib/libgcc_s.so.1
- 00f0b000-00f0c000 rw-p 0001d000 08:0a 524925 /lib/libgcc_s.so.1
- 08048000-08049000 r-xp 00000000 08:0b 1052377 /home/libin/program/C/mem_bug/test
- 08049000-0804a000 r--p 00000000 08:0b 1052377 /home/libin/program/C/mem_bug/test
- 0804a000-0804b000 rw-p 00001000 08:0b 1052377 /home/libin/program/C/mem_bug/test
- 0943e000-0945f000 rw-p 00000000 00:00 0 [heap]
- b7700000-b7721000 rw-p 00000000 00:00 0
- b7721000-b7800000 ---p 00000000 00:00 0
- b78a5000-b78a6000 rw-p 00000000 00:00 0
- b78bc000-b78be000 rw-p 00000000 00:00 0
- bf917000-bf92c000 rw-p 00000000 00:00 0 [stack]
- 已放弃
- *** glibc detected *** ./test: double free or corruption (top): 0x0943e008 ***
参考文献:
DEBUG HACKS