0

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

manner
  • 33
  • 3
  • 1
    Please edit so it is a [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve), showing everything you need to do...including the necessary [initialization of Winsock](https://learn.microsoft.com/en-us/windows/desktop/winsock/initializing-winsock)...and include details of your compiler and platform. It should be possible for people to copy and paste your example into their local compiler and test it to see if they get the same result. Also, be more clear with language like "the stack trace also changes on every run"...changes how? – HostileFork says dont trust SE Nov 06 '18 at 03:24
  • 2
    The code shown works fine for me as-is, no crash, correct DNS results returned in `res`. "*I also tried use deprecated gethostbyname, which gives a similar result...*" - then something on your system is seriously screwed up. `gethostbyname()` only has 1 input parameter, and it must be a null-terminated string or NULL, so the only way to crash it with code is to pass in an invalid string pointer. In that is not the case, then your system has a flawed driver installed, or something else internal is messed up. – Remy Lebeau Nov 06 '18 at 03:30
  • Please show a *complete* code sample. Also explain, what *crash* means to you. If you receive an SEH exception, do record the location where it's raised, the error code, as well as the parameters relating to that error code. – IInspectable Nov 06 '18 at 07:51
  • Works fine (with additional `#include`s, of course) for me. As expected, returns `WSANOTINITIALISED`. – CristiFati Nov 06 '18 at 10:14

1 Answers1

2

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.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148