From e1df69e73e8fe090eda3329f64ca8d80bb2be195 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 13 Aug 2023 15:56:21 -0400 Subject: [PATCH] src: set port in node_options to uint16_t PR-URL: https://github.com/nodejs/node/pull/49151 Reviewed-By: Joyee Cheung --- src/node_options.cc | 21 ++++++++++----------- src/node_options.h | 12 ++++-------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/node_options.cc b/src/node_options.cc index cf44003538f42f..53eafa38b79688 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 @@ -1026,17 +1025,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 eed6216deb67b2..bd48504a99c49c 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -28,24 +28,20 @@ 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 - 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_; - if (other.port_ >= 0) port_ = other.port_; + port_ = other.port_; } private: std::string host_name_; - int port_; + uint16_t port_; }; class Options {