Avoid crashing the db when the ip address is invalid in a REGISTER REPLICA query (#74)
Co-authored-by: jseljan <josip.seljan@memgraph.io>
This commit is contained in:
parent
9966ba1d52
commit
8fc9298832
@ -7,6 +7,7 @@
|
||||
#include "glog/logging.h"
|
||||
|
||||
#include "io/network/endpoint.hpp"
|
||||
#include "io/network/network_error.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace io::network {
|
||||
@ -42,10 +43,16 @@ Endpoint::ParseSocketOrIpAddress(
|
||||
std::vector<std::string> parts = utils::Split(address, delimiter);
|
||||
if (parts.size() == 1) {
|
||||
if (default_port) {
|
||||
if (GetIpFamily(address) == IpFamily::NONE) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::pair{address, *default_port};
|
||||
}
|
||||
} else if (parts.size() == 2) {
|
||||
ip_address = std::move(parts[0]);
|
||||
if (GetIpFamily(ip_address) == IpFamily::NONE) {
|
||||
return std::nullopt;
|
||||
}
|
||||
int64_t int_port{0};
|
||||
try {
|
||||
int_port = utils::ParseInt(parts[1]);
|
||||
@ -77,8 +84,9 @@ Endpoint::Endpoint() {}
|
||||
Endpoint::Endpoint(std::string ip_address, uint16_t port)
|
||||
: address(std::move(ip_address)), port(port) {
|
||||
IpFamily ip_family = GetIpFamily(address);
|
||||
CHECK(ip_family != IpFamily::NONE)
|
||||
<< "Not a valid IPv4 or IPv6 address: " << ip_address;
|
||||
if (ip_family == IpFamily::NONE) {
|
||||
throw NetworkError("Not a valid IPv4 or IPv6 address: {}", ip_address);
|
||||
}
|
||||
family = ip_family;
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ TEST(Endpoint, IPv4) {
|
||||
EXPECT_EQ(endpoint.family, endpoint_t::IpFamily::IP4);
|
||||
|
||||
// test address invalid
|
||||
EXPECT_DEATH(endpoint_t("invalid", 12345), "address");
|
||||
EXPECT_THROW(endpoint_t("invalid", 12345), io::network::NetworkError);
|
||||
}
|
||||
|
||||
TEST(Endpoint, IPv6) {
|
||||
@ -30,7 +30,7 @@ TEST(Endpoint, IPv6) {
|
||||
EXPECT_EQ(endpoint.family, endpoint_t::IpFamily::IP6);
|
||||
|
||||
// test address invalid
|
||||
EXPECT_DEATH(endpoint_t("::g", 12345), "address");
|
||||
EXPECT_THROW(endpoint_t("::g", 12345), io::network::NetworkError);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
Loading…
Reference in New Issue
Block a user