尝试 return 使用 VS2015 和 addrinfoW 的唯一指针时发生堆栈溢出
Stack overflow when trying to return a unique pointer with VS2015 and addrinfoW
我已经编写了一些代码来从 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 returns时,我用VS 2015(我没有其他版本)出现堆栈溢出,我不明白为什么。有人可以帮助我吗?
您传递数组大小(以字节为单位)而不是 swprintf_s
中的数组元素计数,这可能会导致堆栈损坏。在调试模式下,它在用 garbage marker 0xFE 终止 null 后填充缓冲区的其余部分。请注意,swprintf_s
有一个适当的重载来推导 c 样式数组的大小,因此无需显式传递它。
最后的return语句中也不应该有std::move(
。
我已经编写了一些代码来从 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 returns时,我用VS 2015(我没有其他版本)出现堆栈溢出,我不明白为什么。有人可以帮助我吗?
您传递数组大小(以字节为单位)而不是 swprintf_s
中的数组元素计数,这可能会导致堆栈损坏。在调试模式下,它在用 garbage marker 0xFE 终止 null 后填充缓冲区的其余部分。请注意,swprintf_s
有一个适当的重载来推导 c 样式数组的大小,因此无需显式传递它。
最后的return语句中也不应该有std::move(
。