Search code examples
cwindowswinapiwinsock

Winsock getaddrinfo unhandled exception/crash in ntdll.dll


I'm trying to resolve a hostname in C with GetAddrInfoExW()

struct addrinfoexW hints, *res;
int errcode;
void *ptr = 0;

ZeroMemory(&hints, sizeof(struct addrinfoexW));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;

errcode = GetAddrInfoExW(
    L"google.de", // pName
    L"80", // pServiceName
    NS_DNS, // dwNameSpace
    NULL, // lpNspId
    &hints, // hints
    &res, // ppResult
    NULL, // timeout
    NULL, // lpOverlapped
    NULL, // lpCompletionRoutine
    NULL // lpHandle
);
if (errcode != 0)
{
    //perror("getaddrinfo");
    return -1;
}

...but it always crashes on the call to GetAddrInfoEx:

ntdll.dll!RtlAllocateHeap() Unbekannt
mswsock.dll!SockLoadTransportMapping()  Unbekannt
mswsock.dll!SockGetTdiName()    Unbekannt
mswsock.dll!SockSocket()    Unbekannt
mswsock.dll!WSPSocket() Unbekannt
ws2_32.dll!WSASocketW() Unbekannt
ws2_32.dll!GetProtocolStateForFamily()  Unbekannt
ws2_32.dll!GetAddrInfoExW() Unbekannt
Main.exe!lookup_host(const wchar_t * host, addrinfo * out) Zeile 112

It seems all values I pass are correct. I've also tried with GetAddrInfoEx and GetAddrInfo, both in Unicode and Ansi. The stack trace also changes on every run.

I also tried use deprecated gethostbyname, which gives a similar result...

Solved: the heap was corrupted by an earlier allocation i think. Fixed by allocation more space. Thank you all


Solution

  • When you see an exception with RtlAllocateHeap on the top of the stack the error in your code happened much earlier: RltAllocateHeap is indicating that some prior action in the application corrupted heap structures.

    You can verify this by calling HeapValidate before calling GetAddr... etc.