?
Solved

How to find the cause of a segmentation fault?

Posted on 2008-11-06
19
Medium Priority
?
823 Views
Last Modified: 2008-11-29
Hello group,

I just have a small code which sends a request to a host to get an html page. The function (gethtml(a,b) ) sends request by passing two parameters:
1- host name/ip
2- page name

host name and page are stored in nodes of a link list as following:

struct  mystruct {
   char* host;
   char* page
   struct mystruct *next;
}

so I will be running a loop till I get to the ending node. now, the error message I have got is:

Program received signal SIGSEGV, Segmentation fault.
0x0804a258 in get_ip (host=0x8309fa0 "www.mywebsite.com")
    at getpage.c:666
666     if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)

however, the source code is

   if((hent = gethostbyname(host)) == NULL)
    {
      herror("Can't get IP");
    }

    if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)
    {
      perror("Can't resolve host");
      exit(1);
    }


Which line is causing the error? is the first IF or the second IF?

Thanks for your help.

Regards,
ak
0
Comment
Question by:akohan
  • 9
  • 9
19 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22899377
Did you see the "Can't get IP" error ? Why do you continue after encountering this error ?

What is ip ? Is it pointing to valid memory ?
0
 

Author Comment

by:akohan
ID: 22899418

You are right but since I'm new to gdb I thought it has something to do with line 666. When I run gdb it says line 666 which is:

        if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)

Regards.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22899437
>> You are right but since I'm new to gdb I thought it has something to do with line 666.

The segmentation fault occurred at that line, yes. But the actual problem happened before. The two possible causes I mentioned in my previous post are worth investigating.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:akohan
ID: 22899451

Sure. I will look at them and will get back to you.

Thanks.
0
 

Author Comment

by:akohan
ID: 22900705

Hi again!

I found that error happens exactly on this line:
             
            char *ip = (char *)malloc(iplen+1);

Now, what doesn't let this happen?
could be an invalid length (iplen)?
or not enough memory resources?

Thanks.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22900885
check whether ip is NULL after the malloc.
0
 

Author Comment

by:akohan
ID: 22901363

Ok, I spent few hours on this and not going anywhere!
The if statement before malloc() doesn't cause any message so this shows that nothing wrong with ip pointer.

What I get as error is:

Can't get IP: Host name lookup failure.

This is generated by herror() which after specified message adds a colon+blank and then a message about what is happening.

Any idea what could causing this issue?

Regards.



 
    struct hostent *hent;
    int iplen = 15;   
    char *ip = NULL;
    if( (ip = (char *)malloc(iplen+1)) == NULL ){
      printf("\nERROR ALLOCATING ip\n");
      return NULL ;
    }
    memset(ip, 0, iplen+1);
 
    if((hent = gethostbyname(host)) == NULL)
    {
      herror("Can't get IP"); //always shows this message
    }
 
    return ip;

Open in new window

0
 

Author Comment

by:akohan
ID: 22901373

Sorry for the mistake. If is after malloc(). Also, host variable passed to gethostbyname() has a proper value.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22902462
The error simply means that hostname could not be resolved to an IP address. Most common causes of this are incorrect or incomplete hostname or unavailability of resolution service
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22902696
>> What I get as error is:
>> 
>> Can't get IP: Host name lookup failure.

That was one of the two possibilities I mentioned in my fist post ;) If the gethostbyname call didn't succeed, you shouldn't use the returned hent in the call to inet_ntop after that.

As I asked earlier : why are you continuing after encountering that error ? If you encounter an error like this, you can't just ignore it (or you'll get a segmentation fault or something like you got).
0
 

Author Comment

by:akohan
ID: 22908043

Ok, I guss I have been jumping around all day long and confused about it.

SunnyCoder, I checked the host syntax and it is the same as previous request so it seems it is not wrong as far as name concerned.

Also, regarding what Infinity had mentioned that:

>>If the gethostbyname call didn't succeed, you shouldn't use the returned hent in the call to inet_ntop after that.

Do you mean I have to change it above format to the format in snippet?
do you also mean that I shouldn't anything in case of error? because I believe I have to return a NULL to the caller to go on and ignore it.

Please let me know it.

Thanks.





 
 
char* func() {
 
  char *ip = NULL;
  struct hostent *hent;
  int iplen = 15;
 
 
  if( (ip = (char *)malloc(iplen+1)) == NULL ){
    printf("\nERROR ALLOCATING ip\n");
  }else{
    printf("\nip OK!");
  }
 
  memset(ip, 0, iplen+1);
 
 
  if((hent = gethostbyname(host)) == NULL)
  {
    herror("Can't get IP");
    // what should I return here? as far as I know it should be a NULL 
  } else {
 
    if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)
      perror("Can't resolve host");
      // what should I return here? 
  }
  return ip;
 
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908088
>> Do you mean I have to change it above format to the format in snippet?

That would work, yes.


>> do you also mean that I shouldn't anything in case of error? because I believe I have to return a NULL to the caller to go on and ignore it.

What you do when an error occurs, is either gracefully handle the problem or halt if you can't. If the calling code knows that a NULL will be returned in case of failure (and performs a check for NULL before trying to use the ip), then that's a good way of handling this error.
0
 

Author Comment

by:akohan
ID: 22908166

Thanks for your immediate response!

ok, this is the caller:

while(current_page) {
   if( (t = RqPage(current_page->domain, current_page->page)) == NULL)
      //write in whatever is returned
    } else {
       //log the issue and go on
       fprintf(fp, "%s had issue during request!", current_page->domain);
    }
   current_page = current_page->next;
}

Any suggestions?

Regards.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908193
>> Any suggestions?

About what ?
0
 

Author Comment

by:akohan
ID: 22908685

This is driving me nuts ! I'm still getting the message

Can't get IP : host name lookup failure.

What does happen here?

Thanks.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908697
>> I'm still getting the message
>> 
>> Can't get IP : host name lookup failure.

Yes, but no more segmentation fault if you did it correctly, right ?
0
 

Author Comment

by:akohan
ID: 22908918

Unfortunatley, what I get at the end (after almost 3000 requests) is

    Can't get IP: Host name lookup failure
    Segmentation fault

Regards,
ak

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908941
Then it means you didn't handle the error correctly. If you get the "Can't get IP" error, you should NOT use the returned hent, nor should you continue processing that specific connection, because there's no point if you're not able to get the address information.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 22908947
Or in other words : first get rid of the segmentation fault by properly handling the error. Then, you find the cause of the error, and fix that.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question