gethostbyname dumps with SIGSEGV (520 Views)
Reply
Occasional Visitor
David Malkson
Posts: 5
Registered: ‎09-20-1998
Message 1 of 15 (520 Views)

gethostbyname dumps with SIGSEGV

I'm porting an application from Linux to Solaris, and now HP-UX.

The HP-UX port gets SIGSEGV in gethostbyname the second time the function containing it is called.

I'm compiling -Aa -D_HPUX_SOURCE -D_POSIX_SOURCE, linking dynamically, and the program is not threaded.

Any clues where to look?
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Hi:

Verify your arrays are sized correctly and that your pointers are valid.

Regards!

...JRF...
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Can I assume that you are first testing to see if a NULL pointer is returned before referencing any fields within the struct? If not, that would certainly trigger a SIGSEGV.
If it ain't broke, I can fix that.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 4 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

If it's not too big post the code here so we can take the guesswork out of it.
Occasional Visitor
David Malkson
Posts: 5
Registered: ‎09-20-1998
Message 5 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

OK, here's the source:


static
int resolv_hostname( const char *hname,struct sockaddr_in *sin ){
struct hostent *hostp = NULL;
unsigned long addr;

addr = inet_addr( hname );
if (addr != (in_addr_t)-1)
memcpy( &sin->sin_addr, &addr, sizeof( addr ));
else {
hostp = gethostbyname( hname );
if( hostp == NULL )
return -1;
else
memcpy( &sin->sin_addr, hostp->h_addr_list[0], hostp->h_length );
}
return 0;
}

It SIGSEGV's in the gethostbyname call.

The WDB stack says:

strlen+0xc
+ 0x64
+ 0x108
nss_search+0x114
__getghostbyname+0x140
gethostbyname+0x94

all from /usr/lib/libc.2

The hname variable is valid and contains a valid hostname string.

Copying the hname value to a big temporary buffer string makes no difference.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 6 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Some things that you may want to look at. The argument types do not agree with the prototypes of most functions you are using:

change... unsigned long addr;
to... struct in_addr addr;

if ((addr = inet_addr(hname)) == -1)
...
Occasional Visitor
David Malkson
Posts: 5
Registered: ‎09-20-1998
Message 7 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

According to the man page, that should be in_addr_t, not a struct.

I changed it to in_addr_t and it made no difference.

FWIW, the Linux and Solaris ports run just fine and according to all the tools the other OS's have no memory issues.

That leads me to think it is some some include, define, compile switch, or library issue with the HP-UX port.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 8 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Since your program is pretty short why don't you post the entire code along with the list of include'd header files and the command you are using for compiling.

~thanks
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 9 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Ooops hit the submit button prematurely. Trying to clear confusion caused by...

> if ((addr = inet_addr(hname)) == -1)

when I meant...

if ((addr.s_addr = inet_addr(hname)) == -1)

where s_addr is a member of the in_addr structure and is of type in_addr_t.
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 10 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

>According to the man page, that should be in_addr_t, not a struct.

Right.
I had to use in_addr_t so it would work in 64 bit mode.
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 11 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

I assume this is the same as the issue reported in comp.sys.hp.hpux under "gethostbyname core dumps"?

Someone mentioned that h_length in memcpy wasn't valid:
memcpy(&sin->sin_addr, hostp->h_addr_list[0], hostp->h_length);

From the man page, I have no idea whether h_length is the size of h_addr_list[0] or of all of them.

Best to use sizeof(sin->sin_addr).
Occasional Visitor
David Malkson
Posts: 5
Registered: ‎09-20-1998
Message 12 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

Yes, this is the same problem as the USENET posting.

This frament is a small part of a huge program.

WDB doesn't complain about any memory issues, but I just installed the latest SunStudio 12 and ran the Solaris version under it where memory issues are reported elsewhere.

The strange thing is WDB says it's OK and it dumps in the middle of a libc call that has a valid string as a parameter.

At this point the only conclusion I can come to is that the HP implementation of gethostbyname has some area that is reused between calls which is getting hosed.
Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 13 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

>This fragment is a small part of a huge program.

That's why several people and I can't duplicate the abort. Have you linked with -z to catch NULL pointer dereferences?

>WDB doesn't complain about any memory issues

You're using "set heap-check on"?

>The strange thing is WDB says it's OK and it dumps in the middle of a libc call that has a valid string as a parameter.

WDB is only checking the heap usage, not everything. (It relies on the hardware to check the rest. :-)

>At this point the only conclusion I can come to is that the HP implementation of gethostbyname has some area that is reused between calls which is getting hosed.

Possibly.
Are you up to date on patches?

>the second time the function containing it is called.

Are you passing the same string the second time?
Occasional Visitor
David Malkson
Posts: 5
Registered: ‎09-20-1998
Message 14 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

I added a bunch of syslog() calls to the code, now it core dumps in syslog and the stack shows



malloc
malloc_padded
malloc
localtime_r
ctime_r
syslog

I ran a patch assessment and installed the latest patches last week

WDB 5.7 has all memory checks enabled.

CFLAGS = -Ae +w1 +ESnolit -z -g

LIBS = -lxnet -lrtc

This works flawlessly on Solaris.

I'm back to guessing it is a library compatibility issue as it has always dumped in libc.2.

I guess I could put the whole thing up on an anonymous CVS server if noone has any other hints without full sources.

Acclaimed Contributor
Dennis Handly
Posts: 25,277
Registered: ‎03-06-2006
Message 15 of 15 (520 Views)

Re: gethostbyname dumps with SIGSEGV

>now it core dumps in syslog and the stack shows


malloc
malloc_padded
malloc

>WDB 5.7 has all memory checks enabled.

This shows the heap has been corrupted. I'm surprised gdb didn't catch it.

>CFLAGS = -Ae +w1 +ESnolit -z -g

Why are you using +ESnolit? You should use the default or +ESlit.

>I guess I could put the whole thing up on an anonymous CVS server if no one has any other hints without full sources.

I could just try the binary, if you make that available.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.