尝试使用 VS2015 和 addrinfoW 返回唯一指针时堆栈溢出

Stack overflow when trying to return a unique pointer with VS2015 and addrinfoW

本文关键字:指针 唯一 堆栈 栈溢出 返回 addrinfoW VS2015      更新时间:2023-10-16

我已经做了一些代码来从DNS检索地址,但我发生了崩溃。这是我的代码:

#include <ws2tcpip.h>
#include <winSock2.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")
#include <memory>
#include <random>
bool resolveHost(const wchar_t* hostname, uint16_t port);
int main(int argc, char *argv[]) {
    WSADATA wsaData;
    WSAStartup(
      MAKEWORD(2,2),
      &wsaData
    );
    auto l = resolveHost(L"your own DNS here", 80);
}
std::unique_ptr<addrinfoW, decltype (&FreeAddrInfoW)> getAddrInfo(const wchar_t* hostname, quint16 defaultPort)
{
    addrinfoW* result;
    // zero initialization
    addrinfoW hints = {};
    hints.ai_socktype = SOCK_STREAM;
    wchar_t port[9];
    int error = swprintf_s(port, sizeof (port), L"%hu", defaultPort);
    if(error == -1)
    {
        printf("Invalid call to swprintf_s");
        return std::unique_ptr<addrinfoW, decltype (&FreeAddrInfoW)>{nullptr, &FreeAddrInfoW};
    }
    /* resolve the domain name into a list of addresses */
    error = GetAddrInfoW(hostname, port, &hints, &result);
    if (error != 0)
    {
        wprintf(gai_strerror(error));
        return std::unique_ptr<addrinfoW, decltype (&FreeAddrInfoW)>{nullptr, &FreeAddrInfoW};
    }
// here, when I return, I have a stack overflow
    return std::move(std::unique_ptr<addrinfoW, decltype (&FreeAddrInfoW)>{result, &FreeAddrInfoW});
}
bool resolveHost(const wchar_t hostname, uint16_t port)
{
    auto guard {getAddrInfo(hostname, port)};
    return true;
}

当getAddrInfo返回时,我有一个VS 2015的堆栈溢出(我没有另一个版本(,我不明白为什么。有人可以帮助我吗?

以字节为单位传递数组大小,而不是以swprintf_s为单位的数组元素计数,这可能会导致堆栈损坏。在调试模式下,它会在用垃圾标记0xFE终止 null 后填充缓冲区的其余部分。请注意,swprintf_s推断 c 样式数组的大小时,有一个适当的重载,因此无需显式传递它。

此外,在最后一个返回语句中不应有std::move(