Skip to content

Commit

Permalink
Merge pull request grpc#16517 from sreecha/fix-addrlen
Browse files Browse the repository at this point in the history
Fix ipv6 address parsing issue
  • Loading branch information
sreecha authored Sep 10, 2018
2 parents 23a24f5 + 8cf33aa commit 58f6912
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/core/ext/filters/client_channel/parse_address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@ bool grpc_parse_ipv6_hostport(const char* hostport, grpc_resolved_address* addr,
char* host_end = static_cast<char*>(gpr_memrchr(host, '%', strlen(host)));
if (host_end != nullptr) {
GPR_ASSERT(host_end >= host);
char host_without_scope[GRPC_INET6_ADDRSTRLEN];
char host_without_scope[GRPC_INET6_ADDRSTRLEN + 1];
size_t host_without_scope_len = static_cast<size_t>(host_end - host);
uint32_t sin6_scope_id = 0;
if (host_without_scope_len > GRPC_INET6_ADDRSTRLEN) {
gpr_log(GPR_ERROR,
"invalid ipv6 address length %zu. Length cannot be greater than "
"GRPC_INET6_ADDRSTRLEN i.e %d)",
host_without_scope_len, GRPC_INET6_ADDRSTRLEN);
goto done;
}
strncpy(host_without_scope, host, host_without_scope_len);
host_without_scope[host_without_scope_len] = '\0';
if (grpc_inet_pton(GRPC_AF_INET6, host_without_scope, &in6->sin6_addr) ==
Expand Down
14 changes: 14 additions & 0 deletions test/core/client_channel/parse_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ static void test_grpc_parse_ipv6(const char* uri_text, const char* host,
grpc_uri_destroy(uri);
}

/* Test parsing invalid ipv6 addresses (valid uri_text but invalid ipv6 addr) */
static void test_grpc_parse_ipv6_invalid(const char* uri_text) {
grpc_core::ExecCtx exec_ctx;
grpc_uri* uri = grpc_uri_parse(uri_text, 0);
grpc_resolved_address addr;
GPR_ASSERT(!grpc_parse_ipv6(uri, &addr));
grpc_uri_destroy(uri);
}

int main(int argc, char** argv) {
grpc_test_init(argc, argv);
grpc_init();
Expand All @@ -100,5 +109,10 @@ int main(int argc, char** argv) {
test_grpc_parse_ipv6("ipv6:[2001:db8::1]:12345", "2001:db8::1", 12345, 0);
test_grpc_parse_ipv6("ipv6:[2001:db8::1%252]:12345", "2001:db8::1", 12345, 2);

/* Address length greater than GRPC_INET6_ADDRSTRLEN */
test_grpc_parse_ipv6_invalid(
"ipv6:WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW45%"
"v6:45%x$1*");

grpc_shutdown();
}

0 comments on commit 58f6912

Please sign in to comment.