application Crashes on memset ?? any suggestions

Hi All,

we have an application that is written in 'C' programming to connects to various servers in the organization.

The bellow code establish a TCP connection to connect to the remote servers. the application works perfectly ok, but, after some time the entire process get's crashed and dumps a core file. after doing some investigation i found that it crashing when calling a memset function. Please note the entire code is a multi- threaded programming.

int establist_connection(thrd_no, host_name, serv_name, socky, port_no, retry)
int  thrd_no;
char *host_name;
char *serv_name;
int *socky;
int *port_no;
int retry;
{
    struct sockaddr_in serv_add = {0};
    struct sockaddr_in myaddr_in = {0};
    struct hostent *hp = NULL;
    struct servent *sp = NULL;
    struct servent *sRes = NULL;
    struct hostent *hRes = NULL;
    char servBuf[1024];
    char hostBuf[1024];
    int errnop = 0;

   memset ((char *) &serv_add, 0, sizeof(struct sockaddr_in));
    sock_no=0;
   *socky=0;

   /* set up server address structure */
    serv_add.sin_family = AF_INET;

   /* get the remote host information to establish address for server */

   hRes = (struct hostent *)malloc(sizeof(struct hostent));
   hp = gethostbyname_r(host_name, hRes, hostBuf, sizeof(hostBuf), &errnop);

    if(hRes)
     free(hRes);

    if (hp==NULL)
    {
        *socky=0;
        return(-1);
    }

The core shows that the application crashes imdeatly after the gethostbyname_r function is executed, that means it's failing when it's trying to free hRes memory

core DUM
=============
detected a multithreaded program
t@24988 (l@24988) terminated by signal BUS (invalid address alignment)
0x0001d638: establist_connection+0x0108:    ld      [%l0], %l0

dbx: internal error: signal SIGSEGV (no mapping at the fault address)
dbx's coredump will appear in /tmp

Can you please help me with this ... when i google'd around i found some posts that memset could cause the segmentation fault error on Solaris OS. Please share your thoughts on this.

Thank you
Sudharma

Memset is just doing what it is asked for. If a segfault occurs, it's not memset but some bug in your code or a library you use which is more likely to blame.

As you are using dbx, did you try its run time checking functionalities to figure out what is causing that issue ?

See that blog for details on how to enable it: Runtime Memory Checking : Leonard Li's Weblog

Thanks a lot jlliagre