Skip to content

Commit e1df69e

Browse files
anonrigtargos
authored andcommitted
src: set port in node_options to uint16_t
PR-URL: #49151 Reviewed-By: Joyee Cheung <[email protected]>
1 parent fab3906 commit e1df69e

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/node_options.cc

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
#include "openssl/opensslv.h"
1111
#endif
1212

13-
#include <errno.h>
1413
#include <algorithm>
15-
#include <cstdlib> // strtoul, errno
14+
#include <charconv>
1615
#include <limits>
1716
#include <sstream>
1817
#include <string_view>
@@ -1026,17 +1025,17 @@ inline std::string RemoveBrackets(const std::string& host) {
10261025
return host;
10271026
}
10281027

1029-
inline int ParseAndValidatePort(const std::string& port,
1030-
std::vector<std::string>* errors) {
1031-
char* endptr;
1032-
errno = 0;
1033-
const unsigned long result = // NOLINT(runtime/int)
1034-
strtoul(port.c_str(), &endptr, 10);
1035-
if (errno != 0 || *endptr != '\0'||
1036-
(result != 0 && result < 1024) || result > 65535) {
1028+
inline uint16_t ParseAndValidatePort(const std::string_view port,
1029+
std::vector<std::string>* errors) {
1030+
uint16_t result{};
1031+
auto r = std::from_chars(port.data(), port.data() + port.size(), result);
1032+
1033+
if (r.ec == std::errc::result_out_of_range ||
1034+
(result != 0 && result < 1024)) {
10371035
errors->push_back(" must be 0 or in range 1024 to 65535.");
10381036
}
1039-
return static_cast<int>(result);
1037+
1038+
return result;
10401039
}
10411040

10421041
HostPort SplitHostPort(const std::string& arg,

src/node_options.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,20 @@ class HostPort {
2828

2929
void set_host(const std::string& host) { host_name_ = host; }
3030

31-
void set_port(int port) { port_ = port; }
31+
void set_port(uint16_t port) { port_ = port; }
3232

3333
const std::string& host() const { return host_name_; }
3434

35-
int port() const {
36-
// TODO(joyeecheung): make port a uint16_t
37-
CHECK_GE(port_, 0);
38-
return port_;
39-
}
35+
uint16_t port() const { return port_; }
4036

4137
void Update(const HostPort& other) {
4238
if (!other.host_name_.empty()) host_name_ = other.host_name_;
43-
if (other.port_ >= 0) port_ = other.port_;
39+
port_ = other.port_;
4440
}
4541

4642
private:
4743
std::string host_name_;
48-
int port_;
44+
uint16_t port_;
4945
};
5046

5147
class Options {

0 commit comments

Comments
 (0)