C, 웹 크롤러 구현중 ,

Sailor_moon의 이미지

세그멘테이션 폴트가 자주 발생합니다 ..
도와주셨으면 합니다. 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

이거 어떻게 잡을 수 있는거죠 ....

yhsuk의 이미지

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 :) - "여유를 갖고 행동하되 게을러지지 말자"

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.