From 90cdff5a3c7dddea2cce609b5bba5773896dfa2b Mon Sep 17 00:00:00 2001 From: Yijie Ma Date: Thu, 14 Dec 2023 13:01:03 -0800 Subject: [PATCH] [EventEngine] Returns error instead of assertion when hostname is empty (#35306) Closes #35306 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35306 from yijiem:dns-empty-hostname 9c7f67c27ae2860569f95aa056f0dc0ff86f7bce PiperOrigin-RevId: 591028370 --- src/core/lib/event_engine/ares_resolver.cc | 24 ++++++++++++++++--- .../event_engine/test_suite/tests/dns_test.cc | 5 ++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/core/lib/event_engine/ares_resolver.cc b/src/core/lib/event_engine/ares_resolver.cc index 70813eb649a..28568ba20d9 100644 --- a/src/core/lib/event_engine/ares_resolver.cc +++ b/src/core/lib/event_engine/ares_resolver.cc @@ -241,7 +241,13 @@ void AresResolver::LookupHostname( "Unparseable name: ", name))]() mutable { callback(status); }); return; } - GPR_ASSERT(!host.empty()); + if (host.empty()) { + event_engine_->Run([callback = std::move(callback), + status = absl::InvalidArgumentError(absl::StrCat( + "host must not be empty in name: ", + name))]() mutable { callback(status); }); + return; + } if (port_string.empty()) { if (default_port.empty()) { event_engine_->Run([callback = std::move(callback), @@ -307,7 +313,13 @@ void AresResolver::LookupSRV( "Unparseable name: ", name))]() mutable { callback(status); }); return; } - GPR_ASSERT(!host.empty()); + if (host.empty()) { + event_engine_->Run([callback = std::move(callback), + status = absl::InvalidArgumentError(absl::StrCat( + "host must not be empty in name: ", + name))]() mutable { callback(status); }); + return; + } // Don't query for SRV records if the target is "localhost" if (absl::EqualsIgnoreCase(host, "localhost")) { event_engine_->Run([callback = std::move(callback)]() mutable { @@ -336,7 +348,13 @@ void AresResolver::LookupTXT( "Unparseable name: ", name))]() mutable { callback(status); }); return; } - GPR_ASSERT(!host.empty()); + if (host.empty()) { + event_engine_->Run([callback = std::move(callback), + status = absl::InvalidArgumentError(absl::StrCat( + "host must not be empty in name: ", + name))]() mutable { callback(status); }); + return; + } // Don't query for TXT records if the target is "localhost" if (absl::EqualsIgnoreCase(host, "localhost")) { event_engine_->Run([callback = std::move(callback)]() mutable { diff --git a/test/core/event_engine/test_suite/tests/dns_test.cc b/test/core/event_engine/test_suite/tests/dns_test.cc index 587b7094f97..909fb34e03e 100644 --- a/test/core/event_engine/test_suite/tests/dns_test.cc +++ b/test/core/event_engine/test_suite/tests/dns_test.cc @@ -591,4 +591,9 @@ TEST_F(EventEngineDNSTest, UnparseableHostPortsBadLocalhostWithPort) { TestUnparseableHostPort(CreateDNSResolverWithoutSpecifyingServer(), &dns_resolver_signal_, "[localhost]:1"); } + +TEST_F(EventEngineDNSTest, UnparseableHostPortsEmptyHostname) { + TestUnparseableHostPort(CreateDNSResolverWithoutSpecifyingServer(), + &dns_resolver_signal_, ":443"); +} // END