← Back to team overview

maria-developers team mailing list archive

Client connector library crash

 

The new client connector library for C crashes a multi-threaded Windows application. The crash occurs in libmysql.c which uses my_gethostbyname_r, which on Windows, resolves to gethostbyname, which is not thread safe. gethostbyname was deprecated ten years ago in favor of getaddrinfo which is thread safe.

The following code fixes the problem. This fix allows you to remove the file my_gethostbyname.c which was just a cross platform hack that is no longer needed. The following code, which shows both old and new code, is not tested under Linux.

Cheers, and all the best with Maria,
Rob

        #ifdef USE_OLD_GETHOSTBYNAME

        int tmp_errno;
        struct hostent tmp_hostent,*hp;
        char buff2[GETHOSTBYNAME_BUFF_SIZE];
        hp = my_gethostbyname_r(host,&tmp_hostent,buff2,sizeof(buff2),
        &tmp_errno);
        if (!hp)
        {
my_set_error(mysql, CR_UNKNOWN_HOST, SQLSTATE_UNKNOWN, ER(CR_UNKNOWN_HOST),
            host, tmp_errno);
            my_gethostbyname_r_free();
            goto error;
        }
        memcpy(&sock_addr.sin_addr,hp->h_addr, (size_t) hp->h_length);
        my_gethostbyname_r_free();

        #else USE_NEW_GETADDRINFO

        char portbuf[16];
        struct addrinfo hints, *resinfo;
        struct sockaddr_in *ipv;
        memset (&hints, 0, sizeof(hints));
        hints.ai_family = AF_INET; // ipv4 only
if (getaddrinfo (host, itoa (port, portbuf, 10), &hints, &resinfo) != 0){
            goto error;}
        ipv = (struct sockaddr_in *)resinfo->ai_addr;
        memcpy (&sock_addr.sin_addr, &(ipv->sin_addr), 4);
        freeaddrinfo (resinfo);

        #endif






Follow ups

References