akohan
asked on
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
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
ASKER
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.
>> 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.
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.
ASKER
Sure. I will look at them and will get back to you.
Thanks.
ASKER
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.
check whether ip is NULL after the malloc.
ASKER
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;
ASKER
Sorry for the mistake. If is after malloc(). Also, host variable passed to gethostbyname() has a proper value.
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
>> 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).
>>
>> 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).
ASKER
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;
}
>> 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.
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.
ASKER
Thanks for your immediate response!
ok, this is the caller:
while(current_page) {
if( (t = RqPage(current_page->domai
//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.
>> Any suggestions?
About what ?
About what ?
ASKER
This is driving me nuts ! I'm still getting the message
Can't get IP : host name lookup failure.
What does happen here?
Thanks.
>> 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 ?
>>
>> Can't get IP : host name lookup failure.
Yes, but no more segmentation fault if you did it correctly, right ?
ASKER
Unfortunatley, what I get at the end (after almost 3000 requests) is
Can't get IP: Host name lookup failure
Segmentation fault
Regards,
ak
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is ip ? Is it pointing to valid memory ?