segmentation fault #3

  • Follow


hi all~
**********************************************
>cat hostinfo.c

#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
        char **pp;
        struct in_addr addr;
        struct hostent *hostp;

        if (argc != 2)
        {
                fprintf(stderr, "usage: %s <domain name or
dotted-decimal>\n", argv[0]);
                exit(0);
        }

        if (inet_aton(argv[1], &addr) != 0)
                hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
AF_INET);
        else
                hostp = gethostbyname(argv[1]);

        printf("official hostname: %s\n", hostp->h_name);

        for (pp = hostp->h_aliases; *pp != NULL; pp++)
                printf("alias: %s\n", *pp);

        for (pp = hostp->h_addr_list; *pp != NULL; pp++)
        {
                addr.s_addr = *((unsigned int *)*pp);
                printf("address: %s\n", inet_ntoa(addr));
        }

        exit(0);
}
**********************************************
>gcc -o hostinfo hostinfo.c
**********************************************
>./hostinfo 127.0.0.1

official hostname: localhost.localdomain
alias: localhost
address: 127.0.0.1
**********************************************
but
>./hostinfo 192.168.1.1

segmentation fault
**********************************************
why? who could tell me?? thank you very much!!!

0
Reply Final 12/14/2005 8:55:53 AM

Final wrote:
> hi all~
> **********************************************
>> cat hostinfo.c
> 
> #include <netdb.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <stdio.h>
> #include <unistd.h>
> 
> int main(int argc, char **argv)
> {
>         char **pp;
>         struct in_addr addr;
>         struct hostent *hostp;
> 
>         if (argc != 2)
>         {
>                 fprintf(stderr, "usage: %s <domain name or
> dotted-decimal>\n", argv[0]);
>                 exit(0);
>         }
> 
>         if (inet_aton(argv[1], &addr) != 0)
>                 hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
> AF_INET);
>         else
>                 hostp = gethostbyname(argv[1]);
> 
>         printf("official hostname: %s\n", hostp->h_name);

What if gethostbyname cannot translate the address, and returns
NULL ?
(See h_errno and the hstrerror function for help in debugging.)
0
Reply ISO 12/14/2005 9:24:08 AM


Final wrote:
> hi all~
> **********************************************
> 
>>cat hostinfo.c
> 
> 
> #include <netdb.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
> #include <stdio.h>
> #include <unistd.h>
> 
> int main(int argc, char **argv)
> {
>         char **pp;
>         struct in_addr addr;
>         struct hostent *hostp;
> 
>         if (argc != 2)
>         {
>                 fprintf(stderr, "usage: %s <domain name or
> dotted-decimal>\n", argv[0]);
>                 exit(0);
>         }
> 
>         if (inet_aton(argv[1], &addr) != 0)
>                 hostp = gethostbyaddr((const char *)&addr, sizeof(addr),
> AF_INET);
>         else
>                 hostp = gethostbyname(argv[1]);
> 
>         printf("official hostname: %s\n", hostp->h_name);
> 
>         for (pp = hostp->h_aliases; *pp != NULL; pp++)
>                 printf("alias: %s\n", *pp);
> 
>         for (pp = hostp->h_addr_list; *pp != NULL; pp++)
>         {
>                 addr.s_addr = *((unsigned int *)*pp);
>                 printf("address: %s\n", inet_ntoa(addr));
>         }
> 
>         exit(0);
> }
> **********************************************
> 
>>gcc -o hostinfo hostinfo.c
> 
> **********************************************
> 
>>./hostinfo 127.0.0.1
> 
> 
> official hostname: localhost.localdomain
> alias: localhost
> address: 127.0.0.1
> **********************************************
> but
> 
>>./hostinfo 192.168.1.1
> 
> 
> segmentation fault
> **********************************************
> why? who could tell me?? thank you very much!!!
> 

In the statement "for (pp = hostp->h_aliases; *pp != NULL; pp++)",
you're supposed to look for pp != NUll, not *pp != NULL.  As it
stands if pp == NULL, you will try to dereference a NULL pointer,
leading to a SIGSEGV.

--

Fletcher Glenn

0
Reply Fletcher 12/14/2005 4:08:04 PM

2 Replies
156 Views

(page loaded in 0.125 seconds)

Similiar Articles:













7/17/2012 10:24:04 AM


Reply: