• C

How to find the cause of a segmentation fault?

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
akohanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
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
akohanAuthor Commented:

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
Infinity08Commented:
>> 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

akohanAuthor Commented:

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

Thanks.
0
akohanAuthor Commented:

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
Infinity08Commented:
check whether ip is NULL after the malloc.
0
akohanAuthor Commented:

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
akohanAuthor Commented:

Sorry for the mistake. If is after malloc(). Also, host variable passed to gethostbyname() has a proper value.
0
sunnycoderCommented:
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
Infinity08Commented:
>> 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
akohanAuthor Commented:

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
Infinity08Commented:
>> 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
akohanAuthor Commented:

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
Infinity08Commented:
>> Any suggestions?

About what ?
0
akohanAuthor Commented:

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
Infinity08Commented:
>> 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
akohanAuthor Commented:

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
Infinity08Commented:
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
Infinity08Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.