Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add args validation method #56487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 68 additions & 79 deletions src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

#include <cstdlib>


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please remove these unnecessary changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a unit test, but the unnecessary changes are clearly a result of the formatter overzealous behavior. as @jasnell said above, I restored the changes to their original state, yet every time I save the file, it reverts to the modified version.

namespace node {

using v8::Boolean;
Expand Down Expand Up @@ -68,7 +67,6 @@ MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
constructor->NewInstance(env->context(), 1, &type_value));
}


void TCPWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -124,9 +122,7 @@ void TCPWrap::Initialize(Local<Object> target,
NODE_DEFINE_CONSTANT(constants, SERVER);
NODE_DEFINE_CONSTANT(constants, UV_TCP_IPV6ONLY);
NODE_DEFINE_CONSTANT(constants, UV_TCP_REUSEPORT);
target->Set(context,
env->constants_string(),
constants).Check();
target->Set(context, env->constants_string(), constants).Check();
}

void TCPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
Expand Down Expand Up @@ -174,15 +170,13 @@ void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
new TCPWrap(env, args.This(), provider);
}


TCPWrap::TCPWrap(Environment* env, Local<Object> object, ProviderType provider)
: ConnectionWrap(env, object, provider) {
int r = uv_tcp_init(env->event_loop(), &handle_);
CHECK_EQ(r, 0); // How do we proxy this error up to javascript?
// Suggestion: uv_tcp_init() returns void.
}


void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -192,7 +186,6 @@ void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}


void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -205,7 +198,6 @@ void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}


#ifdef _WIN32
void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
Expand All @@ -217,7 +209,6 @@ void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
}
#endif


void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
Expand All @@ -228,8 +219,7 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
int fd = static_cast<int>(val);
int err = uv_tcp_open(&wrap->handle_, fd);

if (err == 0)
wrap->set_fd(fd);
if (err == 0) wrap->set_fd(fd);

args.GetReturnValue().Set(err);
}
Expand Down Expand Up @@ -259,9 +249,8 @@ void TCPWrap::Bind(
int err = uv_ip_addr(*ip_address, port, &addr);

if (err == 0) {
err = uv_tcp_bind(&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
flags);
err = uv_tcp_bind(
&wrap->handle_, reinterpret_cast<const sockaddr*>(&addr), flags);
}
args.GetReturnValue().Set(err);
}
Expand All @@ -270,50 +259,46 @@ void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
Bind<sockaddr_in>(args, AF_INET, uv_ip4_addr);
}


void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
Bind<sockaddr_in6>(args, AF_INET6, uv_ip6_addr);
}


void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
TCPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
Environment* env = wrap->env();
int backlog;
if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
backlog,
OnConnection);
int err = uv_listen(
reinterpret_cast<uv_stream_t*>(&wrap->handle_), backlog, OnConnection);
args.GetReturnValue().Set(err);
}


void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
CHECK(args[2]->IsUint32());
// explicit cast to fit to libuv's type expectation
int port = static_cast<int>(args[2].As<Uint32>()->Value());
Connect<sockaddr_in>(args,
[port](const char* ip_address, sockaddr_in* addr) {
return uv_ip4_addr(ip_address, port, addr);
Environment* env = Environment::GetCurrent(args);
int port;
if (!args[2]->Int32Value(env->context()).To(&port)) return;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new update here

Connect<sockaddr_in>(args, [port](const char* ip_address, sockaddr_in* addr) {
return uv_ip4_addr(ip_address, port, addr);
});
}


void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[2]->IsUint32());

int port;
if (!args[2]->Int32Value(env->context()).To(&port)) return;
Connect<sockaddr_in6>(args,
[port](const char* ip_address, sockaddr_in6* addr) {
return uv_ip6_addr(ip_address, port, addr);
});
return uv_ip6_addr(ip_address, port, addr);
});
}

template <typename T>
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
void TCPWrap::Connect(
const FunctionCallbackInfo<Value>& args,
std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -390,61 +375,65 @@ MaybeLocal<Object> AddressToJS(Environment* env,

int port;

if (info.IsEmpty())
info = Object::New(env->isolate());
if (info.IsEmpty()) info = Object::New(env->isolate());

switch (addr->sa_family) {
case AF_INET6:
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
// Add an interface identifier to a link local address.
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr) && a6->sin6_scope_id > 0) {
const size_t addrlen = strlen(ip);
CHECK_LT(addrlen, sizeof(ip));
ip[addrlen] = '%';
size_t scopeidlen = sizeof(ip) - addrlen - 1;
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
const int r = uv_if_indextoiid(a6->sin6_scope_id,
ip + addrlen + 1,
&scopeidlen);
if (r) {
env->ThrowUVException(r, "uv_if_indextoiid");
return {};
case AF_INET6:
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
// Add an interface identifier to a link local address.
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr) && a6->sin6_scope_id > 0) {
const size_t addrlen = strlen(ip);
CHECK_LT(addrlen, sizeof(ip));
ip[addrlen] = '%';
size_t scopeidlen = sizeof(ip) - addrlen - 1;
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
const int r =
uv_if_indextoiid(a6->sin6_scope_id, ip + addrlen + 1, &scopeidlen);
if (r) {
env->ThrowUVException(r, "uv_if_indextoiid");
return {};
}
}
}
port = ntohs(a6->sin6_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip)).Check();
info->Set(env->context(), env->family_string(), env->ipv6_string()).Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port)).Check();
break;

case AF_INET:
a4 = reinterpret_cast<const sockaddr_in*>(addr);
uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
port = ntohs(a4->sin_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip)).Check();
info->Set(env->context(), env->family_string(), env->ipv4_string()).Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port)).Check();
break;

default:
info->Set(env->context(),
env->address_string(),
String::Empty(env->isolate())).Check();
port = ntohs(a6->sin6_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip))
.Check();
info->Set(env->context(), env->family_string(), env->ipv6_string())
.Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port))
.Check();
break;

case AF_INET:
a4 = reinterpret_cast<const sockaddr_in*>(addr);
uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
port = ntohs(a4->sin_port);
info->Set(env->context(),
env->address_string(),
OneByteString(env->isolate(), ip))
.Check();
info->Set(env->context(), env->family_string(), env->ipv4_string())
.Check();
info->Set(env->context(),
env->port_string(),
Integer::New(env->isolate(), port))
.Check();
break;

default:
info->Set(env->context(),
env->address_string(),
String::Empty(env->isolate()))
.Check();
}

return scope.Escape(info);
}


} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(tcp_wrap, node::TCPWrap::Initialize)
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-net-localerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ connect({
port: 0,
localPort: 'foobar',
}, 'ERR_INVALID_ARG_TYPE', TypeError);

// Test invalid connection string
assert.throws(
() => connect('string', () => { }),
TypeError
);
Loading