Skip to content

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

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:improve-url-overrun
Mar 10, 2023
Merged

url: fix array overrun in node:url::SetArgs()#47001
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:improve-url-overrun

Conversation

@anonrig

Copy link
Copy Markdown
Member

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
Copy Markdown
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-botnodejs-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
anonrigforce-pushed the improve-url-overrun branch from b099ef0 to 4d5188aCompareMarch 7, 2023 21:52
@anonriganonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 7, 2023
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 7, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@TimothyGuTimothyGu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small suggestion; otherwise lgtm.

Comment threadsrc/node_url.cc
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@anonriganonrig 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
@TimothyGuTimothyGu removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 8, 2023

@RaisinTenRaisinTen left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

joyeecheung commented Mar 9, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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

@anonriganonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 10, 2023
@nodejs-github-botnodejs-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:mainMar 10, 2023
@nodejs-github-bot

Copy link
Copy Markdown
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>
@targostargos mentioned this pull request Mar 14, 2023
@danielleadams

Copy link
Copy Markdown
Contributor

blocked by #46736

@anonriganonrig mentioned this pull request Jun 5, 2023
19 tasks
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>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.c++Issues and PRs that require attention from people who are familiar with C++.needs-ciPRs that need a full CI run.whatwg-urlIssues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@anonrig@nodejs-github-bot@bnoordhuis@joyeecheung@addaleax@danielleadams@lemire@TimothyGu@RaisinTen