From 404a7b33211d33eabff245a83936db70fda24779 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 13 Aug 2023 15:56:21 -0400 Subject: [PATCH 1/2] src: set port in node_options to uint16_t --- src/node_options.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/node_options.h b/src/node_options.h index bc18a45e681a3c..444309f68642d0 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -28,24 +28,23 @@ class HostPort { void set_host(const std::string& host) { host_name_ = host; } - void set_port(int port) { port_ = port; } + void set_port(uint16_t port) { port_ = port; } const std::string& host() const { return host_name_; } - int port() const { - // TODO(joyeecheung): make port a uint16_t + uint16_t port() const { CHECK_GE(port_, 0); return port_; } void Update(const HostPort& other) { if (!other.host_name_.empty()) host_name_ = other.host_name_; - if (other.port_ >= 0) port_ = other.port_; + port_ = other.port_; } private: std::string host_name_; - int port_; + uint16_t port_; }; class Options { From f10e1aac52ab776820d7bb4609aabacba2a1ff8f Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 14 Aug 2023 11:14:28 -0400 Subject: [PATCH 2/2] fixup! src: set port in node_options to uint16_t --- src/node_options.cc | 21 ++++++++++----------- src/node_options.h | 5 +---- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/node_options.cc b/src/node_options.cc index b544f1209143c0..2f11e57e957ee6 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -10,9 +10,8 @@ #include "openssl/opensslv.h" #endif -#include #include -#include // strtoul, errno +#include #include #include #include @@ -1010,17 +1009,17 @@ inline std::string RemoveBrackets(const std::string& host) { return host; } -inline int ParseAndValidatePort(const std::string& port, - std::vector* errors) { - char* endptr; - errno = 0; - const unsigned long result = // NOLINT(runtime/int) - strtoul(port.c_str(), &endptr, 10); - if (errno != 0 || *endptr != '\0'|| - (result != 0 && result < 1024) || result > 65535) { +inline uint16_t ParseAndValidatePort(const std::string_view port, + std::vector* errors) { + uint16_t result{}; + auto r = std::from_chars(port.data(), port.data() + port.size(), result); + + if (r.ec == std::errc::result_out_of_range || + (result != 0 && result < 1024)) { errors->push_back(" must be 0 or in range 1024 to 65535."); } - return static_cast(result); + + return result; } HostPort SplitHostPort(const std::string& arg, diff --git a/src/node_options.h b/src/node_options.h index 444309f68642d0..3ff5e207d651ac 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -32,10 +32,7 @@ class HostPort { const std::string& host() const { return host_name_; } - uint16_t port() const { - CHECK_GE(port_, 0); - return port_; - } + uint16_t port() const { return port_; } void Update(const HostPort& other) { if (!other.host_name_.empty()) host_name_ = other.host_name_;