C, 웹 크롤러 구현중 ,
글쓴이: Sailor_moon / 작성시간: 수, 2012/02/08 - 12:45오후
세그멘테이션 폴트가 자주 발생합니다 ..
도와주셨으면 합니다. dump 된 파일을 보려고 했는데 분명히 -g 옵션을 주고 했는데도 불구하고 , 덤프 파일을 인식을 못하네요 ...
말 그대로 유저가 입력한 url 과 , 그 url 이 들어갈 경로 path 를 입력받아서 ,
시스템 명령어인 wget 을 이용해서 저장하는 함수입니다.
char *getPage(char* url, int depth, char* path){ char dir[20]; char cmd[200]; // these arraies are for command line char* page; // return values size_t len; sprintf(dir, "%stemp.html", path); //fl_counter is global _variable sprintf(cmd, "wget -q -O %s %s", dir, url); //creating command to grab web-page using wget //char cmd[30]="wget -q -O "; //char* fileName = "temp"; //strcat(cmd, path); //strcat(cmd," "); //strcat(cmd, url); system(cmd); //The wget command will grab index html file FILE *fl = fopen(dir,"rb"); //open downloaded template html file if (fl == NULL) { printf("grabbing from web-site %s failed !\n", url); exit(1); // fail and exit out } fseek(fl, 0 , SEEK_END); //move fl pointer to last len = ftell(fl); rewind(fl); page = (char *)malloc(sizeof(char)*len+1); // allocate memory to read //page[len] = '\0'; fread(page, sizeof(char)*len,1, fl); fclose(fl); free(page); // free ... return page; }
위와 같은 함수에서 html 파일을 담고있는 스트링을 넘겨받으면 ,...
이제 URL만을 긁어오기 위해 우선 화이트 스페이스를 제거하려 했습니다.
page = getPage(argv[1] , atoi(argv[3]) , argv[2]) ; 해서 받아오고 removed_white = removeWhiteSpace(page) 넘겼는데 ... ... void removeWhiteSpace(char* html) { int i; char *buffer = malloc(strlen(html)+1), *p =malloc(sizeof(char) +1); .. ... memset(buffer, 0 , strlen(html)+1); Program received signal SIGSEGV, Segmentation fault. 0x0000000000401790 in removeWhiteSpace ( html=0x7ffff7fd8010 <Address 0x7ffff7fd8010 out of bounds>) at html.c:322 322 char *buffer = malloc(strlen(html)+1), *p=malloc (sizeof(char)+1); (gdb) print page_Result No symbol "page_Result" in current context. (gdb) quit A debugging session is active
이거 어떻게 잡을 수 있는거죠 ....
Forums:
free된 메모리를 참조해서
free된 메모리를 참조해서 그렇습니다.
getPage 내에서 문자열 버퍼를 malloc으로 할당했다 free시키고, 그 버퍼를 리턴했네요.
page = (char *)malloc(sizeof(char)*len+1); // allocate memory to read
//page[len] = '\0';
fread(page, sizeof(char)*len,1, fl);
fclose(fl);
free(page); // free ... <===== free된 버퍼 포인터 리턴.
return page;
gdb에서도 파라미터인 html이 범위를 벗어난 주소를 참조하고 있다고 에러를 출력해 주네요.
(html=0x7ffff7fd8010 )
Signature :) - "여유를 갖고 행동하되 게을러지지 말자"
댓글 달기