Address review comments on port validation.

PiperOrigin-RevId: 946184592
This commit is contained in:
Edwin Wu
2026-07-11 06:05:52 -07:00
committed by Copybara-Service
parent 02354e2968
commit 2aff5d38e0
8 changed files with 363 additions and 25 deletions
+2 -2
View File
@@ -37,9 +37,9 @@ void ServiceAddressToProto(
bool ServiceAddressFromProto(
const location::nearby::connections::ServiceAddress& proto,
ServiceAddress& service_address) {
// Address must be either 4 or 16 bytes and port must be set.
// Address must be either 4 or 16 bytes and port must be valid (1 to 65535).
if ((proto.ip_address().size() != 16 && proto.ip_address().size() != 4) ||
proto.port() == 0) {
proto.port() <= 0 || proto.port() > 65535) {
return false;
}
service_address.address = {proto.ip_address().begin(),
+11 -1
View File
@@ -81,8 +81,18 @@ TEST(ServiceAddressTest, ServiceAddressFromProtoInvalidAddress) {
TEST(ServiceAddressTest, ServiceAddressFromProtoInvalidPort) {
ProtoServiceAddress proto;
proto.set_ip_address(std::string("\x7f\0\0\1", 4));
proto.set_port(0);
ServiceAddress service_address;
// Port 0
proto.set_port(0);
EXPECT_FALSE(ServiceAddressFromProto(proto, service_address));
// Port > 65535
proto.set_port(65536);
EXPECT_FALSE(ServiceAddressFromProto(proto, service_address));
// Port < 0
proto.set_port(-1);
EXPECT_FALSE(ServiceAddressFromProto(proto, service_address));
}