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

url: fix array overrun in node:url::SetArgs() #47001

Merged
merged 1 commit into from
Mar 10, 2023
Merged
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
58 changes: 17 additions & 41 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::Undefined;
using v8::Value;

Local<String> Utf8String(Isolate* isolate, const std::string& str) {
Expand All @@ -46,18 +45,20 @@ enum url_update_action {
kHref = 9,
};

void SetArgs(Environment* env, Local<Value> argv[10], const ada::result& url) {
void SetArgs(Environment* env,
Local<Value> (*argv)[10],
const ada::result& url) {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally this parameter should be an const ada::url&. It doesn't make sense to pass an ada::result in when we always expect it to be filled with the "valid" value.

In fact the call sites should probably change from this:

  ada::result out = ada::parse(input.ToStringView());
  CHECK(out);
  out->set_protocol(…);

to

  ada::result out = ada::parse(input.ToStringView());
  CHECK(out);
  const ada::url& url = out.value();
  url.set_protocol(…);

Copy link
Member Author

Choose a reason for hiding this comment

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

out.value() returns a copy. The current implementation looks unsafe, but CHECK(out) ensures that it is not. I tried to avoid returning a copy. @lemire wrote about this on Ada's discussion board: ada-url/ada#200

Copy link
Member

Choose a reason for hiding this comment

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

I think you misunderstood ada-url/ada#200. out.value() (and equivalently *out) does not return a copy by itself. It returns a reference, so if you put it into a const ada::url& then no copies are made.

On the other hand, ada::url url = *out, which would make a copy. That's not what I'm suggesting here.

Alternatively, you can also do

  ada::result out = ada::parse(input.ToStringView());
  CHECK(out);
  ada::url url = std::move(*out);

which is written under the "performance tip". It would also avoid a copy.

Copy link
Member

Choose a reason for hiding this comment

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

It is likely that the current code is safe and efficient. I think we are discussing 'coding style' which is subjective.

I don't personally find the url->... annoying.

I am slightly triggered by the (*argv)[..] however. :-)

What about using a reference to an array instead?

Local<Value> (&argv)[10]

(The ampersand would be dropped from the calling site.)

Copy link
Member

Choose a reason for hiding this comment

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

It's a style guide thing: pointers for things that are mutated, const references otherwise.

With mutable references it's sometimes ambiguous to a reader if code operates on the original or on a copy. With pointers, no such ambiguity exists.

Isolate* isolate = env->isolate();
argv[0] = Utf8String(isolate, url->get_href());
argv[1] = Utf8String(isolate, url->get_origin());
argv[2] = Utf8String(isolate, url->get_protocol());
argv[3] = Utf8String(isolate, url->get_hostname());
argv[4] = Utf8String(isolate, url->get_pathname());
argv[5] = Utf8String(isolate, url->get_search());
argv[6] = Utf8String(isolate, url->get_username());
argv[7] = Utf8String(isolate, url->get_password());
argv[8] = Utf8String(isolate, url->get_port());
argv[9] = Utf8String(isolate, url->get_hash());
(*argv)[0] = Utf8String(isolate, url->get_href());
(*argv)[1] = Utf8String(isolate, url->get_origin());
(*argv)[2] = Utf8String(isolate, url->get_protocol());
(*argv)[3] = Utf8String(isolate, url->get_hostname());
(*argv)[4] = Utf8String(isolate, url->get_pathname());
(*argv)[5] = Utf8String(isolate, url->get_search());
(*argv)[6] = Utf8String(isolate, url->get_username());
(*argv)[7] = Utf8String(isolate, url->get_password());
(*argv)[8] = Utf8String(isolate, url->get_port());
(*argv)[9] = Utf8String(isolate, url->get_hash());
}

void Parse(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -69,7 +70,6 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
Local<Function> success_callback_ = args[2].As<Function>();

Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

Expand All @@ -89,20 +89,8 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(false);
}

const Local<Value> undef = Undefined(isolate);
Local<Value> argv[] = {
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
};
SetArgs(env, argv, out);
Local<Value> argv[10];
SetArgs(env, &argv, out);
USE(success_callback_->Call(
env->context(), args.This(), arraysize(argv), argv));
args.GetReturnValue().Set(true);
Expand Down Expand Up @@ -235,20 +223,8 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
}
}

const Local<Value> undef = Undefined(isolate);
Local<Value> argv[] = {
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
undef,
};
SetArgs(env, argv, out);
Local<Value> argv[10];
SetArgs(env, &argv, out);
USE(success_callback_->Call(
env->context(), args.This(), arraysize(argv), argv));
args.GetReturnValue().Set(result);
Expand Down