1
0
mirror of synced 2025-10-20 02:29:24 +03:00

Fix UB by use of dangling references in getaddrinfo_with_timeout (#2232)

* Fix use of dangling references

When the resolve thread is detached, local variables were still used, which could lead to a program crash.

* Add test to verify dangling ref fix

* Add missing brace initialization

* Assert that the remaining fields are really zeroed

* Fix use of chrono literals
This commit is contained in:
Jonas van den Berg
2025-09-15 02:05:09 +02:00
committed by GitHub
parent f72b4582e6
commit 6e52d0a057
2 changed files with 61 additions and 16 deletions

View File

@@ -1331,6 +1331,27 @@ TEST(RangeTest, FromHTTPBin_Online) {
}
}
TEST(GetAddrInfoDanglingRefTest, LongTimeout) {
auto host = "unresolvableaddress.local";
auto path = std::string{"/"};
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
SSLClient cli(host, port);
#else
auto port = 80;
Client cli(host, port);
#endif
cli.set_connection_timeout(1);
{
auto res = cli.Get(path);
ASSERT_FALSE(res);
}
std::this_thread::sleep_for(std::chrono::seconds(8));
}
TEST(ConnectionErrorTest, InvalidHost) {
auto host = "-abcde.com";