2

I ran into a problem trying to test an application under Application Verifier with Page Heap on. It turns out that gethostbyname API always fails even for legit host names like "localhost". The problem reproduces on every Win-7 or Server 2008 R2 I tried even for a very simple test applications using gethostbyname.

Repro steps: in appverifier check "page heap" and "UseLFGGuard..." checkboxes, run any app using gethostbyname(..).

appverif flags

Example of application code (prints "127.0.0.1" when appverifier is off, "getaddrinfo failed" when appverifier is on):

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

void
Exercise()
{
    int i = 0;

    struct hostent *remoteHost;
    struct in_addr addr;

    remoteHost = gethostbyname("localhost");
    if (remoteHost == NULL)
    {
        printf("gethostbyname(localhost) failed\n");
    }
    else
    {
        if (remoteHost->h_addrtype == AF_INET)
        {
            i=0;
            while (remoteHost->h_addr_list[i] != 0)
            {
                addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
                printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
            }
        }
        else
        {
            printf("unexpected address type\n");
        }
    }
}

int
main()
{
    WSADATA wsaData;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0)
    {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    for(int i=0; i<1000; i++)
    {
        Exercise();
        Sleep(1000);
    }

    return 0;
}

The most unusual thing is that I could not find anything in the internet. Is this a known problem? Any workarounds?

glagolig
  • 1,100
  • 1
  • 12
  • 28
  • When the call fails, what is the value returned by `WSAGetLastError`? It should show you why the call failed, which may answer your question. Also, `gethostbyname` is deprecated, you should instead be using `getaddrinfo`. – icabod Sep 06 '13 at 11:44
  • 1
    11001 WSAHOST_NOT_FOUND. But the host "localhost" is found just fine when pageheap is off. The same happens with for any other valid host name. I'm not using gethostbyname in new code, but there are hundreds of occurrences in existing code and libraries. – glagolig Sep 06 '13 at 17:23

0 Answers0