• C

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)
chsalviaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fridomCEO/ProgrammerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
fridomCEO/ProgrammerCommented:
Debian, AFAIK is Ubuntu Debian based.

Regards
Friedrich
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.