Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

Valgrind reports "Invalid Read" on gethostbyname()

Valgrind reports invalid read errors on the gethostbyname() function call in the C socket library.  The program works fine, but I'm wondering if this is just a problem with valgrind, or if I'm doing something wrong.  The following small code, which simply creates a new socket and then calls gethostbyname() is enough to generate the error:

int port = 80;

int main() {
      int m_sock;
      sockaddr_in m_addr;

      m_sock = socket(AF_INET, SOCK_STREAM, 0);
      int on = 1;
      setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on));

      struct hostent* hp;

      if ((hp = gethostbyname("www.google.com"))) {
            memset((char*) &m_addr, 0, sizeof(m_addr));
            memmove((char*) &m_addr.sin_addr, hp->h_addr, hp->h_length);
      }

      m_addr.sin_family = AF_INET;
      m_addr.sin_port = htons(port);
}

Is there anything wrong with the above code, or is this likely a problem with valgrind?  (In case it helps. I'm currently running ubuntu Linux)
0
chsalvia
Asked:
chsalvia
  • 2
1 Solution
 
fridomCommented:
After getting a clean compile with the following code:
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>




int port = 80;

int main() {
      int m_sock;
      struct sockaddr_in m_addr;

      m_sock = socket(AF_INET, SOCK_STREAM, 0);
      int on = 1;
      setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(on));

      struct hostent* hp;

      if ((hp = gethostbyname("www.google.com"))) {
            memset((char*) &m_addr, 0, sizeof(m_addr));
            memmove((char*) &m_addr.sin_addr, hp->h_addr, hp->h_length);
      }

      m_addr.sin_family = AF_INET;
      m_addr.sin_port = htons(port);
      return 0;
}

I get this as output:
valgrind ./a.out
==19981== Memcheck, a memory error detector.
==19981== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==19981== Using LibVEX rev 1606, a library for dynamic binary translation.
==19981== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==19981== Using valgrind-3.2.0-Debian, a dynamic binary instrumentation framework.
==19981== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==19981== For more details, rerun with: -v
==19981==
==19981==
==19981== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
==19981== malloc/free: in use at exit: 0 bytes in 0 blocks.
==19981== malloc/free: 56 allocs, 56 frees, 9,009 bytes allocated.
==19981== For counts of detected errors, rerun with: -v
==19981== All heap blocks were freed -- no leaks are possible.

So I can not see where the problem should be, just that your code is not compilable

Regards
Friedrich
0
 
chsalviaAuthor Commented:
Friedrich,

Thanks for the response.  What system are you running this under?  I'm wondering if the implementation of gethostbyname in ubuntu is the problem.
0
 
fridomCommented:
Debian, AFAIK is Ubuntu Debian based.

Regards
Friedrich
0

Featured Post

Industry Leaders: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now