Skip to content

fs: do basic arg validation of truncate in js - #2498

Closed
thefourtheye wants to merge 1 commit into
nodejs:masterfrom
thefourtheye:fs-improve-validation
Closed

fs: do basic arg validation of truncate in js#2498
thefourtheye wants to merge 1 commit into
nodejs:masterfrom
thefourtheye:fs-improve-validation

Conversation

@thefourtheye

Copy link
Copy Markdown
Contributor

This patch moves the basic validation of arguments to truncate family
of functions to the JavaScript from the C++ layer.

cc @bnoordhuis

CI Run: https://jenkins-iojs.nodesource.com/job/node-test-pull-request/131/

@thefourtheyethefourtheye added the fs Issues and PRs related to the fs subsystem / file system. label Aug 22, 2015
Comment threadlib/fs.js Outdated

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 Number.isFinite does both checks in one call

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@targos Ah, you are correct. I updated now. PTAL.

@mscdex

Copy link
Copy Markdown
Contributor

Out of curiousity, what was the motivation behind these changes?

@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

Comment threadlib/fs.js Outdated

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.

Just seeing the error message. If you want to ensure it is an integer, you can drop Number.isFinite and use Number.isInteger instead

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@targos Man, that's wonderful. I didn't know about that function and that is exactly what we need here. Thanks :-) Updated the PR.

@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

@bnoordhuis Ping!

Comment threadlib/fs.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change in functionality. Here's an approximate test:

if(typeofpath==='number'&&!Number.isInteger(path))thrownewTypeError('Not an integer');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or are you just short-circuiting for the fast path?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This is a legacy functionality it seems. If we pass an fd to truncate it internally delegates it to ftruncate. So we cannot throw error here. We are just checking if it is a positive integer and move on to the next check if is not.

Are you suggesting to not to call ftruncate at all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just followed the logic paths for current and this patch, and there seemed to be a discrepancy.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@trevnorris Agree, but wouldn't this check be better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My assumption was to land most these changes on a minor, then making any breaking changes, that push this to semver-major, to another PR. If you're cool with this staying on major then I'll have another look from that perspective.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@trevnorris I am okay with this being in major :-) Let's get this done right. Please suggest improvements as well :-)

@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

Bump!

Comment threadsrc/node_file.cc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change this and the below line to:

CHECK(args[1]->IsNumber());
constint64_t = args[1]->IntegerValue();

Reason for the CHECK() is because we're now making an assumption about the incoming value. Always want to catch that if incorrect, because at this point if the value is incorrect then we've screwed up.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

wait. nm. read this as the fd value, not the len value.

EDIT: meaning, no need for the CHECK(). can still collapse the other.

@trevnorris

Copy link
Copy Markdown
Contributor

to recap

  • Change all fd conversion in native from Int32Value() to Uint32Value().
  • Change all fd checks in JS to check if valid uint32
    • Will probably need to do a fd = +fd at the top of the function for string coercion. This is simply so we can have a solid native CHECK() to make sure we didn't screw up.
  • Make sure there is a CHECK(val->IsUint32()) just before doing Uint32Value() to all fd's.

@Fishrock123

Copy link
Copy Markdown
Contributor

ping @thefourtheye

@jasnelljasnell added the stalled Issues and PRs that are stalled. label Nov 16, 2015
@targos

Copy link
Copy Markdown
Member

ping @thefourtheye ;)

@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

@targos Ah, I have this open for a loooong time. I'll get to it this week. Thanks for reminding :-) Last week @benjamingr also reminded me of this.

@thefourtheye
thefourtheyeforce-pushed the fs-improve-validation branch 2 times, most recently from 000eee8 to 8233f9aCompareFebruary 12, 2016 19:11
@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

@trevnorris Sorry for the delay in getting your review comments addressed. PTAL.

@jasnelljasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Feb 13, 2016
@jasnell

Copy link
Copy Markdown
Member

Added semver-major tag based on the discussion thread. Feel free to change that if it turns out not to be major after all.

Comment threadlib/fs.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change this to a TypeError. Also I'd replace "positive" with "unsigned".

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Done!

@thefourtheye
thefourtheyeforce-pushed the fs-improve-validation branch 2 times, most recently from 6036668 to 3aab026CompareMarch 14, 2016 16:39
@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

@trevnorris Changed positive to unsigned now.

@trevnorris

Copy link
Copy Markdown
Contributor

Thanks for the change, and test. LGTM.

@thefourtheye
thefourtheyeforce-pushed the fs-improve-validation branch 2 times, most recently from 7a953f9 to f19c403CompareJuly 21, 2016 04:30
@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

Rebased and improved the isFD function. CI Run: https://ci.nodejs.org/job/node-test-pull-request/3365/

This patch moves the basic validation of arguments to `truncate` family
of functions to the JavaScript from the C++ layer.
@thefourtheye
thefourtheyeforce-pushed the fs-improve-validation branch from f19c403 to 693f14eCompareJuly 21, 2016 21:50
@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

Rebased, after landing #7168. CI Run: https://ci.nodejs.org/job/node-test-pull-request/3378/

@thefourtheye

Copy link
Copy Markdown
ContributorAuthor

Landed in c86c1ee.

@thefourtheye
thefourtheye deleted the fs-improve-validation branch July 21, 2016 22:32
thefourtheye added a commit that referenced this pull request Jul 21, 2016
This patch
1. moves the basic validation of arguments to `truncate` family
of functions to the JavaScript layer from the C++ layer.
2. makes sure that the File Descriptors are validated strictly.
PR-URL: #2498
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Jul 22, 2016
This reverts commit c86c1ee.
original commit message:
This patch
1. moves the basic validation of arguments to `truncate` family
of functions to the JavaScript layer from the C++ layer.
2. makes sure that the File Descriptors are validated strictly.
PR-URL: nodejs#2498
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@Fishrock123Fishrock123 removed the stalled Issues and PRs that are stalled. label Jul 26, 2016
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Aug 4, 2016
This reverts commit c86c1ee.
original commit message:
This patch
1. moves the basic validation of arguments to `truncate` family
of functions to the JavaScript layer from the C++ layer.
2. makes sure that the File Descriptors are validated strictly.
PR-URL: nodejs#2498
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: nodejs#7950
Reviewed-By: Julien Gilli <jgilli@nodejs.org>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@gibfahngibfahn mentioned this pull request Jun 15, 2017
3 tasks
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fsIssues and PRs related to the fs subsystem / file system.semver-majorPRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@thefourtheye@mscdex@trevnorris@Fishrock123@targos@jasnell@addaleax