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

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Mar 7, 2023

I am following up on @tniessen's pull request and @bnoordhuis's comments. @TimothyGu, thanks for the mention & reminder

Reference: #46541 (comment)

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Mar 7, 2023
@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 7, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 7, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

Small suggestion; otherwise lgtm.

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.

@anonrig anonrig added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Mar 8, 2023
@TimothyGu TimothyGu removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 8, 2023
Copy link
Contributor

@RaisinTen RaisinTen left a comment

Choose a reason for hiding this comment

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

The current fix LGTM but it feels like a C-style fix. Since this is a C++ file, we could also leverage the power of C++:

Since the problem described in #46541 (comment) is about array arguments decaying to pointers, why don't we use std::array here instead? The doc states:

Unlike a C-style array, it doesn't decay to T* automatically.

Doing that should also address https://github.com/nodejs/node/pull/47001/files#r1130218455:

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

@bnoordhuis
Copy link
Member

@RaisinTen per convention, the std::array would have to be passed by pointer, so IMO it doesn't provide much benefit. More elaboration in #47001 (comment).

@joyeecheung
Copy link
Member

joyeecheung commented Mar 9, 2023

Or you could just return a std::vector<Local<Value> and do Call(...., argv.size(), argv.data()) later ;) I doubt there is any meaningful performance difference between the two, and no one needs to actually count the number of arguments in the code that way, and you can get rid of the undef fillers in the caller.

@addaleax
Copy link
Member

addaleax commented Mar 9, 2023

You could also just return a std::array? That would make intention clear better than an out parameters does, and would still avoid the dynamic memory allocation from std::vector (in fact, I'd expect it to compile to pretty much the same code as the current state of this PR).

@anonrig
Copy link
Member Author

anonrig commented Mar 10, 2023

Since there isn't any request for changes on this pull request, I'll add a commit-queue label.

@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 10, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 10, 2023
@nodejs-github-bot nodejs-github-bot merged commit 72e971e into nodejs:main Mar 10, 2023
@nodejs-github-bot
Copy link
Collaborator

Landed in 72e971e

@anonrig anonrig deleted the improve-url-overrun branch March 10, 2023 14:11
addaleax added a commit to addaleax/node that referenced this pull request Mar 10, 2023
targos pushed a commit that referenced this pull request Mar 13, 2023
PR-URL: #47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Mar 13, 2023
Implements a review suggestion from 72e971e.

Refs: #47001 (comment)
PR-URL: #47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
targos pushed a commit that referenced this pull request Mar 14, 2023
PR-URL: #47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
targos pushed a commit that referenced this pull request Mar 14, 2023
Implements a review suggestion from 72e971e.

Refs: #47001 (comment)
PR-URL: #47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@danielleadams
Copy link
Member

blocked by #46736

@danielleadams danielleadams added the backport-blocked-v18.x PRs that should land on the v18.x-staging branch but are blocked by another PR's pending backport. label Apr 3, 2023
anonrig added a commit to anonrig/node that referenced this pull request Jun 5, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jun 5, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@anonrig anonrig added backport-open-v18.x Indicate that the PR has an open backport. and removed backport-blocked-v18.x PRs that should land on the v18.x-staging branch but are blocked by another PR's pending backport. labels Jun 5, 2023
anonrig added a commit to anonrig/node that referenced this pull request Jun 23, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jun 23, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
anonrig added a commit to anonrig/node that referenced this pull request Jun 23, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jun 23, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
anonrig added a commit to anonrig/node that referenced this pull request Jun 26, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jun 26, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
anonrig added a commit to anonrig/node that referenced this pull request Jul 6, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jul 6, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
anonrig added a commit to anonrig/node that referenced this pull request Jul 9, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jul 9, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
anonrig added a commit to anonrig/node that referenced this pull request Jul 9, 2023
PR-URL: nodejs#47001
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
anonrig pushed a commit to anonrig/node that referenced this pull request Jul 9, 2023
Implements a review suggestion from 72e971e.

Refs: nodejs#47001 (comment)
PR-URL: nodejs#47035
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
danielleadams pushed a commit that referenced this pull request Jul 12, 2023
PR-URL: #47001
Backport-PR-URL: #48345
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
danielleadams pushed a commit that referenced this pull request Jul 12, 2023
Implements a review suggestion from 72e971e.

Refs: #47001 (comment)
PR-URL: #47035
Backport-PR-URL: #48345
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@danielleadams danielleadams added backported-to-v18.x PRs backported to the v18.x-staging branch. and removed backport-open-v18.x Indicate that the PR has an open backport. labels Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. backported-to-v18.x PRs backported to the v18.x-staging branch. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants