From ad3956b657bc491fa941908a52d7bbb0bc088dd1 Mon Sep 17 00:00:00 2001 From: Han Fei Date: Thu, 31 Oct 2019 00:08:51 +0800 Subject: [PATCH] replace `gethostbyname` by a thread-safe version `gethostbyname_r` --- dbms/src/Storages/Transaction/PDTiKVClient.cpp | 15 +++++++++++++-- dbms/src/Storages/Transaction/PDTiKVClient.h | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dbms/src/Storages/Transaction/PDTiKVClient.cpp b/dbms/src/Storages/Transaction/PDTiKVClient.cpp index 80b400dc024..c154f3c00a3 100644 --- a/dbms/src/Storages/Transaction/PDTiKVClient.cpp +++ b/dbms/src/Storages/Transaction/PDTiKVClient.cpp @@ -1,6 +1,7 @@ #include #include + #include namespace DB @@ -24,7 +25,7 @@ std::string getIP(const std::string & address) // convertAddr converts host name to network address. // We assume the converted net type is AF_NET. -std::string convertAddr(const std::string & address) +std::string IndexReader::convertAddr(const std::string & address) { if (address.size() == 0) return ""; @@ -33,8 +34,18 @@ std::string convertAddr(const std::string & address) return ""; auto host = address.substr(0, idx); auto port = address.substr(idx + 1); - struct hostent * result = gethostbyname(host.data()); + + char buf[1024]; + struct hostent buff_storage; + struct hostent * result; + int err = 0; + gethostbyname_r(host.data(), &buff_storage, buf, sizeof(buf), &result, &err); // Suppose we always use IPv4 address. + if (result == nullptr || result->h_addr == nullptr || err != 0) + { + LOG_ERROR(log, "Cannot resolve host name: " << address << " error message is :" << hstrerror(err)); + return ""; + } std::string addr; for (int i = 0; i < 4; i++) { diff --git a/dbms/src/Storages/Transaction/PDTiKVClient.h b/dbms/src/Storages/Transaction/PDTiKVClient.h index 43d9b9cbc0e..3e4e3947d14 100644 --- a/dbms/src/Storages/Transaction/PDTiKVClient.h +++ b/dbms/src/Storages/Transaction/PDTiKVClient.h @@ -33,6 +33,8 @@ struct IndexReader : public pingcap::kv::RegionClient const std::vector & learners, pingcap::kv::RpcCallPtr rpc); + + std::string convertAddr(const std::string & address); }; using IndexReaderPtr = std::shared_ptr;