Search

strcat()

[- Disclaimer -] 아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
strcat()
✦ <string.h>
✦ Destination 문자열 내 NULL String 제거 후 Source 문자열 이어 쓰기 복사
✦ Parameter1에 전달되는 Argument1
✧ Destination 문자열 주소
✦ Parameter2에 전달되는 Argument2
✧ Source 문자열 주소
✦ Return 값
✧ 성공
→ Destination 문자열
✧ 실패
→ 0
✦ BOF 취약 함수
✧ 문자열 크기 미검증
→ 할당된 공간을 넘어서도 문자열 길이를 검증하지 않아 계속 이어쓰기 복사/덮어쓰기가 가능하며 이 경우 Error가 날 수도 안 날 수도 있음
char *strcat(char *strDestination, const char *strSource);
Plain Text
복사
Ex)
#include <stdio.h> #include <string.h> int main(void) { char buf[10] = "abc"; char* string = "def"; strcat(buf, string); printf("%s", buf); return 0; }
Plain Text
복사
abcdef
Plain Text
복사
strcat() 기능 구현 - Ex)
#include <stdio.h> char *self_strcat(char *destination, const char *source); int main(void) { char buf[10] = "abc"; char *string = "def"; self_strcat(buf, string); printf("%s", buf); return 0; } char *self_strcat(char *destination, const char *source) { int i = 0; int j = 0; while(destination[i] != '\0') ++i; while(source[j] != '\0') { destination[i] = source[j]; i++; j++; } destination[i] = '\0'; return destination; }
Plain Text
복사
abcdef
Plain Text
복사