Copilot commented on code in PR #3736:
URL: https://github.com/apache/brpc/pull/3736#discussion_r4537288395
##########
test/endpoint_unittest.cpp:
##########
@@ -260,6 +260,21 @@ TEST(EndPointTest, ipv6) {
ASSERT_EQ(-1, butil::str2endpoint("[]:", &ep));
}
+TEST(EndPointTest, ipv6_endpoint2hostname_port) {
+ butil::EndPoint ep;
+ ASSERT_EQ(0, butil::str2endpoint("[::1]:8080", &ep));
+ char host[256];
+ if (butil::endpoint2hostname(ep, host, sizeof(host)) != 0) {
+ GTEST_SKIP() << "::1 can't be resolved to a name on this machine";
+ }
+ // The port should be in host byte order as the one in endpoint2str.
+ ASSERT_TRUE(butil::StringPiece(host).ends_with(":8080")) << host;
+
+ std::string host_str;
+ ASSERT_EQ(0, butil::endpoint2hostname(ep, &host_str));
+ ASSERT_EQ(host, host_str);
Review Comment:
`ASSERT_EQ(host, host_str)` compares `const char*` to `std::string` in an
order that may not compile (there is `std::string == const char*`, but not
necessarily `const char* == std::string`). To make the comparison unambiguous
and correct, compare as strings (e.g., `ASSERT_EQ(std::string(host),
host_str)`) or use `ASSERT_STREQ(host, host_str.c_str())`.
##########
src/butil/details/extended_endpoint.hpp:
##########
@@ -315,7 +315,7 @@ class ExtendedEndPoint {
}
size_t len = ::strlen(host);
if (len + 1 < host_len) {
- snprintf(host + len, host_len - len, ":%d", _u.in6.sin6_port);
+ snprintf(host + len, host_len - len, ":%d",
ntohs(_u.in6.sin6_port));
Review Comment:
Since this header now directly uses `ntohs`, ensure the header is
self-contained with respect to the declaration/platform portability of `ntohs`
(avoid relying on indirect includes or include order). Also, the format string
uses `:%d` for a port value; it’s more appropriate to print it as unsigned
(e.g., `:%u`) with an explicit cast to avoid signed formatting and make intent
clear.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]