Frage

I am trying to use gethostbyname() function in C for a networking assignment.

When I try to do this

struct hostent *host;
char* addr = malloc (10*sizeof(char));  
strcpy (addr, "localhost");  
host= (struct hostent *) gethostbyname(addr);  

I get the following error.

glibc detected ./Sender: free(): invalid next size (normal): 0x0000000001584480 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fcdbaca4b96]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x155)[0x7fcdbac94815]
/lib/x86_64-linux-gnu/libc.so.6(+0x103b16)[0x7fcdbad29b16]
/lib/x86_64-linux-gnu/libc.so.6(__res_maybe_init+0x125)[0x7fcdbad2b3d5]
/lib/x86_64-linux-gnu/libc.so.6(__nss_hostname_digits_dots+0x3d)[0x7fcdbad2db0d]
/lib/x86_64-linux-gnu/libc.so.6(gethostbyname+0x8b)[0x7fcdbad32b7b]
./Sender[0x401858]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fcdbac4776d]
./Sender[0x400a09]
======= Memory map: ========
00400000-00403000 r-xp 00000000 08:07 3573874                            /home/karthik/courses/Sem 6/Networks/Lab/lab4/Sender
00602000-00603000 r--p 00002000 08:07 3573874                            /home/karthik/courses/Sem 6/Networks/Lab/lab4/Sender  
00603000-00604000 rw-p 00003000 08:07 3573874                            /home/karthik/courses/Sem 6/Networks/Lab/lab4/Sender
01584000-015a5000 rw-p 00000000 00:00 0                                  [heap]
7fcdbaa10000-7fcdbaa25000 r-xp 00000000 08:08 525634                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcdbaa25000-7fcdbac24000 ---p 00015000 08:08 525634                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcdbac24000-7fcdbac25000 r--p 00014000 08:08 525634                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcdbac25000-7fcdbac26000 rw-p 00015000 08:08 525634                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcdbac26000-7fcdbaddb000 r-xp 00000000 08:08 541113                     /lib/x86_64-linux-gnu/libc-2.15.so
7fcdbaddb000-7fcdbafda000 ---p 001b5000 08:08 541113                     /lib/x86_64-linux-gnu/libc-2.15.so
7fcdbafda000-7fcdbafde000 r--p 001b4000 08:08 541113                     /lib/x86_64-linux-gnu/libc-2.15.so
7fcdbafde000-7fcdbafe0000 rw-p 001b8000 08:08 541113                     /lib/x86_64-linux-gnu/libc-2.15.so
7fcdbafe0000-7fcdbafe5000 rw-p 00000000 00:00 0 
7fcdbafe5000-7fcdbb007000 r-xp 00000000 08:08 545991                     /lib/x86_64-linux-gnu/ld-2.15.so
7fcdbb1e4000-7fcdbb1e7000 rw-p 00000000 00:00 0 
7fcdbb203000-7fcdbb207000 rw-p 00000000 00:00 0 
7fcdbb207000-7fcdbb208000 r--p 00022000 08:08 545991                     /lib/x86_64-linux-gnu/ld-2.15.so
7fcdbb208000-7fcdbb20a000 rw-p 00023000 08:08 545991                     /lib/x86_64-linux-gnu/ld-2.15.so
7fff5c673000-7fff5c695000 rw-p 00000000 00:00 0                          [stack]
7fff5c7ff000-7fff5c800000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)`

How do I solve this problem?

War es hilfreich?

Lösung

There's a memory corruption somewhere in your code, in a part you probably didn't show. Since your snippet isn't valid C, I assume you typed it manually or copied it incomplete.

To debug such scenarios, if you are on a platform that supports it (as you are), use either DrMemory or Valgrind. I prefer Valgrind for its features, whereas DrMemory is arguably faster.

Both programs will be able to pinpoint the problem and even attach you to a debugger while the problematic program is still running. So you can inspect error conditions within the live program.


Side-note: if you don't have to assemble the name in any way, you can pass it as a literal string to gethostbyname:

struct hostent *host;
host = (struct hostent *) gethostbyname("localhost");

Andere Tipps

The first line is missing a trailing ;.

If it's not that the OP is really using the propably uninitialised address instead of addr this smells like a memory corruption having happend before the actual call to gethostbyname().

Under the assumption that

  • the use of address is a typo and
  • <netdb.h> is #includeed

and besides the missing check for malloc()'s succeeding, the OP's code is correct.

And could look like this:

#include <netdb.h>

...

struct hostent * host = NULL;
char * addr = malloc (10 * sizeof(*addr));  
if (!addr)
  perror("malloc");
else 
{
  strcpy (addr, "localhost");  
  host = gethostbyname(addr);  
  ...

So you might like to use a memory checker like valgrind so see what is going wrong.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top