Thread: help with seg fault

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    help with seg fault

    hi all.

    Can someone explain why U get a segmentation fault in the following block of code

    Code:
    int setup(char *hostname, int ttl, SOCKET *icmpSock, SOCKADDR_IN *dest)
    {
        HOSTENT *hp;
        unsigned int addr;
        int sockRet;
        
        // create socket
        *icmpSock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0);
        if (*icmpSock == INVALID_SOCKET) {
           printf("Could not create socket : %d.\n", WSAGetLastError());
           return -1;
        }
        
        // setup for TTL
        sockRet = setsockopt(*icmpSock, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
        if (sockRet == SOCKET_ERROR) {
           printf("ttl setsockopt failed : %d. \n", WSAGetLastError());
           return -1;
        }
        
        memset(&dest, 0, sizeof(*dest));
        addr = inet_addr(hostname);
        if (addr == INADDR_NONE) {
           hp = gethostbyname(hostname);
           if (hp != 0) {
              //memcpy(&dest->sin_addr, hp->h_addr, hp->h_length);
              dest->sin_addr = *((struct in_addr *)hp->h_addr);
              dest->sin_family = hp->h_addrtype;
           } else {
              printf("unknown hostname || failed to resolve\n");
              return -1;
           }
        } else {
            dest->sin_addr.s_addr = addr;
            dest->sin_family = AF_INET;
        }
        
        return 0;
    }
    The error is coming from the line dest->sin_addr = *((struct in_addr *)hp->h_addr);
    I get the same error if I try to use memcpy as above it.

    dest is a pointer to an empty structure.

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > memset(&dest, 0, sizeof(*dest));
    Remove the &
    You just made your pointer a NULL pointer, and hosed a bunch of memory following it as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Thanks Salem.

    When you get a sec, can you just quickly explain why the pointer was set null and what happened to the memory.

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    2.14.6 memset

    Declaration:

    Code:
    void *memset(void *str, int c, size_t n);
    Copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.

    The argument str is returned.
    memset(dest, 0, sizeof(*dest));
    The second argument is what is being filled into dest

  5. #5
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    I was using this page as a learning reference http://tangentsoft.net/wskfaq/examples/rawping.html

    In their function setup_for_ping they set the address of dest to 0 as I did initially. (I see why thats wrong now. Thanks sand_man.)

    This is quite a popular paper and if theirs was wrong, I would have though someone would have picked up on it.

    So assuming theirs is right, what makes mine different than theirs?
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps because yours is an inaccurate translation of the C++ code (on that page) to the C code (you posted)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM