// 1. 함수 호출시 Stack의 모양을 정확히 알아두세요!
// 2. 배열의잘못된 index 가 실행시 문제가 될수있다 (컴파일시 확인 안됨)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 아래 함수는 안전한가?
void foo2(const char* p)
{
char buf[10];
strcpy(buf,p); // ?? 안전하지 않은함수
//방법 1 strncpy 사용
strncpy(buf,p,9);
//방법 2 char* buf=(char*)malloc(strlen(p)+1);
strcpy(buf,p);
}
void goo() {printf("goo\n"); exit(0);}
void foo(int a, int b)
{
int x=0;
int y[2];
y[4] = (int)goo; //어디가 바뀔까요 ?
//------------------------------------
y[2] = 10; // 컴파일원리 (*(y+2)=10)
//y[7] =20; 버퍼 오버 플로우 발생돼지 않게 조심해야 한다
printf("%d\n",x); //얼마 일까요?? 왜 10 ??
return ; //어디로 돌아갈지 어떻게 알까?
}
void main()
{
int n=10;
foo(1,2); // push 2; push 1;
//call _foo -> push 돌아갈주소; (jmp _foo;)
foo2("123456789aaabbbcccdddd");
}
'프로그래밍' 카테고리의 다른 글
Error Handling (0) | 2009.07.19 |
---|---|
Dos Compilering & Make Asm (0) | 2009.07.19 |
Asm Ex File (0) | 2009.07.19 |
배열에 C언어 연구 (0) | 2009.07.19 |
c언어로 짠 싸이월드 (0) | 2009.07.19 |
댓글