Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Valgrind reports "Invalid Read" on gethostbyname()

Posted on 2007-03-18
3
Medium Priority
?
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 1000 total points
ID: 18748745
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
ID: 18749954
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
ID: 18754303
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

604 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