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)
{
auto * ptr_records = reinterpret_cast<std::unordered_set<std::string>*>(arg);
if (status == ARES_SUCCESS && host->h_aliases)
auto * ptr_records = static_cast<std::unordered_set<std::string>*>(arg);
if (ptr_records && status == ARES_SUCCESS)
{
/*
* In some cases (e.g /etc/hosts), hostent::h_name is filled and hostent::h_aliases is empty.
@ -28,11 +28,14 @@ namespace DB
ptr_records->insert(ptr_record);
}
int i = 0;
while (auto * ptr_record = host->h_aliases[i])
if (host->h_aliases)
{
ptr_records->insert(ptr_record);
i++;
int i = 0;
while (auto * ptr_record = host->h_aliases[i])
{
ptr_records->insert(ptr_record);
i++;
}
}
}
}