我试图了解堆栈损坏时会发生什么。这是我试图理解的示例程序。我已将缓冲区的大小定义为 1 个字节。但是堆栈损坏发生在我输入第 13 个字节之后。为什么在第 13 个字节后会损坏?
C代码:
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[]){
char buffer[1];
strcpy(buffer,argv[1]);
printf("\n buffer : %s \n",buffer);
return 0;
}
汇编代码:
.file "buffer_overflow.c"
.section .rodata
.LC0:
.string "\n buffer : %s \n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl 12(%ebp), %eax
addl $4, %eax
movl (%eax), %eax
movl %eax, 4(%esp)
leal 31(%esp), %eax
movl %eax, (%esp)
call strcpy
movl $.LC0, %eax
leal 31(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5"
.section .note.GNU-stack,"",@progbits