From fb42afbbaca54360acc3d17becdc53c241acddf4 Mon Sep 17 00:00:00 2001 From: Arthur Passos Date: Thu, 1 Sep 2022 08:59:14 -0300 Subject: [PATCH] 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. --- src/Common/CaresPTRResolver.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Common/CaresPTRResolver.cpp b/src/Common/CaresPTRResolver.cpp index e5d48b864c8..a02909309b6 100644 --- a/src/Common/CaresPTRResolver.cpp +++ b/src/Common/CaresPTRResolver.cpp @@ -15,8 +15,8 @@ namespace DB static void callback(void * arg, int status, int, struct hostent * host) { - auto * ptr_records = reinterpret_cast*>(arg); - if (status == ARES_SUCCESS && host->h_aliases) + auto * ptr_records = static_cast*>(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++; + } } } }