Operating System - Linux
1752790 Members
6223 Online
108789 Solutions
New Discussion юеВ

Re: gethostbyname dumps with SIGSEGV

 
David Malkson
New Member

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?
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor

Re: gethostbyname dumps with SIGSEGV

Hi:

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

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

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.
Sandman!
Honored Contributor

Re: gethostbyname dumps with SIGSEGV

If it's not too big post the code here so we can take the guesswork out of it.
David Malkson
New Member

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.
Sandman!
Honored Contributor

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)
...
David Malkson
New Member

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.
Sandman!
Honored Contributor

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
Sandman!
Honored Contributor

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.
Dennis Handly
Acclaimed Contributor

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.