8000 net, refactor: detects port arguments passed as strings in new TcpSer… · fibjs/fibjs@4be9147 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 4be9147

Browse files
committed
net, refactor: detects port arguments passed as strings in new TcpServer to avoid incorrect handling.
1 parent f035fad commit 4be9147

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

fibjs/include/inetAddr.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,24 @@ union inetAddr {
131131
}
132132
};
133133

134+
inline int32_t get_port(const char* str)
135+
{
136+
int32_t n = 0;
137+
138+
if (!str)
139+
return 0;
140+
141+
if (*str < '1' || *str > '9')
142+
return -1;
143+
144+
n = *str++ - '0';
145+
while (*str >= '0' && *str <= '9')
146+
n = n * 10 + *str++ - '0';
147+
148+
if (*str)
149+
return -1;
150+
151+
return n;
152+
}
153+
134154
} /* namespace fibjs */

fibjs/src/http/HttpServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ result_t HttpServer_base::_new(exlib::string addr, int32_t port, Handler_base* h
3636
result_t HttpServer_base::_new(exlib::string addr, Handler_base* hdlr,
3737
obj_ptr<HttpServer_base>& retVal, v8::Local<v8::Object> This)
3838
{
39+
int32_t n = get_port(addr.c_str());
40+
41+
if (n >= 0)
42+
return _new("", n, hdlr, retVal, This);
3943
return _new(addr, 0, hdlr, retVal, This);
4044
}
4145

fibjs/src/net/TcpServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ result_t TcpServer_base::_new(exlib::string addr, int32_t port,
4444
result_t TcpServer_base::_new(exlib::string addr, Handler_base* listener,
4545
obj_ptr<TcpServer_base>& retVal, v8::Local<v8::Object> This)
4646
{
47+
int32_t n = get_port(addr.c_str());
48+
49+
if (n >= 0)
50+
return _new_tcpServer("", n, listener, retVal, This);
4751
return _new_tcpServer(addr, 0, listener, retVal, This);
4852
}
4953

0 commit comments

Comments
 (0)
0