Solved

Valgrind reports "Invalid Read" on gethostbyname()

Posted on 2007-03-18
3
494 Views
Last Modified: 2008-01-09
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
Comment
Question by:chsalvia
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
Comment Utility
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
 

Author Comment

by:chsalvia
Comment Utility
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
 
LVL 24

Expert Comment

by:fridom
Comment Utility
Debian, AFAIK is Ubuntu Debian based.

Regards
Friedrich
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now