GeezerVison is showing github.com — src: set port in node_options to uint16_t · nodejs/node@e1df69e
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_options.cc +10-11Lines changed: 10 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
10 | 10 | #include "openssl/opensslv.h" |
11 | 11 | #endif |
12 | 12 |
|
13 | | -#include <errno.h> |
14 | 13 | #include <algorithm> |
15 | | -#include <cstdlib> // strtoul, errno |
| 14 | +#include <charconv> |
16 | 15 | #include <limits> |
17 | 16 | #include <sstream> |
18 | 17 | #include <string_view> |
@@ -1026,17 +1025,17 @@ inline std::string RemoveBrackets(const std::string& host) { |
1026 | 1025 | return host; |
1027 | 1026 | } |
1028 | 1027 |
|
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)) { |
1037 | 1035 | errors->push_back(" must be 0 or in range 1024 to 65535."); |
1038 | 1036 | } |
1039 | | - return static_cast<int>(result); |
| 1037 | + |
| 1038 | + return result; |
1040 | 1039 | } |
1041 | 1040 |
|
1042 | 1041 | HostPort SplitHostPort(const std::string& arg, |
|
Collapse file
Copy file name to clipboardExpand all lines: src/node_options.h +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,24 +28,20 @@ class HostPort { |
28 | 28 |
|
29 | 29 | void set_host(const std::string& host) { host_name_ = host; } |
30 | 30 |
|
31 | | - void set_port(int port) { port_ = port; } |
| 31 | + void set_port(uint16_t port) { port_ = port; } |
32 | 32 |
|
33 | 33 | const std::string& host() const { return host_name_; } |
34 | 34 |
|
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_; } |
40 | 36 |
|
41 | 37 | void Update(const HostPort& other) { |
42 | 38 | if (!other.host_name_.empty()) host_name_ = other.host_name_; |
43 | | - if (other.port_ >= 0) port_ = other.port_; |
| 39 | + port_ = other.port_; |
44 | 40 | } |
45 | 41 |
|
46 | 42 | private: |
47 | 43 | std::string host_name_; |
48 | | - int port_; |
| 44 | + uint16_t port_; |
49 | 45 | }; |
50 | 46 |
|
51 | 47 | class Options { |
|