CaresPTRResolver small safety improvement

Previous to #40769, only `hostent::h_aliases` was being accessed. After that PR got merged, `hostent::h_name` started being accessed as well. This PR moves the first `hostent::h_aliases != nullptr` check that could prevent `hostent::h_name` from being accessed. During debugging, I observed that even when there are not aliases, `hostent::h_aliases` is not null. That's why it hasn't caused any problems, but proposing this change to be on the safe side.
This commit is contained in:
Arthur Passos 2022-09-01 08:59:14 -03:00 committed by GitHub
parent 29ac78a92b
commit fb42afbbac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,8 +15,8 @@ namespace DB
static void callback(void * arg, int status, int, struct hostent * host) static void callback(void * arg, int status, int, struct hostent * host)
{ {
auto * ptr_records = reinterpret_cast<std::unordered_set<std::string>*>(arg); auto * ptr_records = static_cast<std::unordered_set<std::string>*>(arg);
if (status == ARES_SUCCESS && host->h_aliases) if (ptr_records && status == ARES_SUCCESS)
{ {
/* /*
* In some cases (e.g /etc/hosts), hostent::h_name is filled and hostent::h_aliases is empty. * In some cases (e.g /etc/hosts), hostent::h_name is filled and hostent::h_aliases is empty.
@ -28,6 +28,8 @@ namespace DB
ptr_records->insert(ptr_record); ptr_records->insert(ptr_record);
} }
if (host->h_aliases)
{
int i = 0; int i = 0;
while (auto * ptr_record = host->h_aliases[i]) while (auto * ptr_record = host->h_aliases[i])
{ {
@ -36,6 +38,7 @@ namespace DB
} }
} }
} }
}
CaresPTRResolver::CaresPTRResolver(CaresPTRResolver::provider_token) : channel(nullptr) CaresPTRResolver::CaresPTRResolver(CaresPTRResolver::provider_token) : channel(nullptr)
{ {