GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: add test for `position` validation in `fs.read()` and `fs.readSync()` by LiviaMedeiros · Pull Request #42837 · nodejs/node

Conversation

@LiviaMedeiros

@LiviaMedeiros LiviaMedeiros commented Apr 23, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR adds a test for position validation in fs.read() and fs.readSync() methods.

To be expanded in: #42835 (filehandle.read() and cases where method tries to read bytes beyond int64 limit)

Outdated stuff This PR replaced `EINVAL` from `read(2)` syscall with `RangeError` exception, when `fs.read()` or `fs.readSync()` is called with huge bigint `position`, and adding `length` makes it bigger than `2⁶³ - 1`.
import { openSync, readSync } from 'fs';
const fd = openSync('/mnt/flashdrive/last_digits_of_pi');
const buffer = Buffer.alloc(9);
const result = readSync(fd, buffer, { position: 2n ** 63n - 5n, length: 9 });
node:fs:723
  handleErrorFromBinding(ctx);
  ^

Error: EINVAL: invalid argument, read
    at readSync (node:fs:723:3)
    at file:///tmp/test.mjs:4:16
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  errno: -22,
  syscall: 'read',
  code: 'EINVAL'
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Apr 23, 2022
@LiviaMedeiros LiviaMedeiros mentioned this pull request Apr 23, 2022
fs: adjust position validation in reading methods #42835
Merged
aduh95
aduh95 reviewed Apr 23, 2022
View reviewed changes

@aduh95 aduh95 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can we add a test?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LiviaMedeiros reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
aduh95
aduh95 reviewed Apr 23, 2022
View reviewed changes
Comment thread test/parallel/test-fs-read-position-validation.mjs Outdated

await testValid(2n ** 63n - 1n - BigInt(length));
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n);
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n - BigInt(length));

@aduh95 aduh95 Apr 23, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It looks like passing 2n ** 63n - BigInt(length) works on master (on both the sync and the async version), I'm not seeing any EINVAL error 🤔 maybe it's OS specific or something? If that's the case, it's probably not worth doing it in a separate PR, it's probably easier to land it alongside the other changes as semver-major.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LiviaMedeiros LiviaMedeiros Apr 23, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Huh. Well, it definitely depends on read syscall implementation.
I retried this on three Node.js versions:

import { fileURLToPath } from 'url';
import { openSync, readSync } from 'fs';
const fd = openSync(fileURLToPath(import.meta.url));
const buffer = Buffer.alloc(9);
readSync(fd, buffer, { position: 2n ** 63n - 15n, length: 9 }); console.log(buffer); // should work
readSync(fd, buffer, { position: 2n ** 63n - 5n, length: 9 }); console.log(buffer); // might fail with EINVAL
readSync(fd, buffer, { position: 2n ** 63n + 5n, length: 9 }); console.log(buffer); // must fail with exception

v14.17.6 most likely doesn't support bigints and assumes current position so it reads from the start:

<Buffer 69 6d 70 6f 72 74 20 7b 20>
<Buffer 66 69 6c 65 55 52 4c 54 6f>
<Buffer 50 61 74 68 20 7d 20 66 72>

v16.14.1 and master (v19.0.0-pre) both show this:

<Buffer 00 00 00 00 00 00 00 00 00>
node:fs:732
  handleErrorFromBinding(ctx);
  ^

Error: EINVAL: invalid argument, read
    at readSync (node:fs:732:3)
    at file:///tmp/chk.mjs:6:1
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  errno: -22,
  syscall: 'read',
  code: 'EINVAL'
}

This is on 64bit linux 5.17.0.

If the - 5n line works on any other platform, it is definitely semver-major indeed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LiviaMedeiros LiviaMedeiros Apr 23, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Tried v18.0.0 release build on 64bit windows 10.0.18363, same result

>node.exe c:\chk.mjs
<Buffer 00 00 00 00 00 00 00 00 00>
node:fs:732
  handleErrorFromBinding(ctx);
  ^

Error: EINVAL: invalid argument, read
    at readSync (node:fs:732:3)
    at file:///c:/chk.mjs:6:1
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  errno: -4071,
  syscall: 'read',
  code: 'EINVAL'
}

Node.js v18.0.0

Out of curiosity, on which platform it works successfully?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aduh95 aduh95 Apr 23, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I’ve tested it on macOS. Could we try to revert the changes in lib, change the test case to catch EINVAL, and run the CI to see which platforms behave like this? (in case that’s just an oddity of my machine or if I’ve tested it wrong somehow)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aduh95 aduh95 Apr 23, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So it looks like the behavior is indeed platform specific. I suggest we keep in this PR all the tests that pass on master and make the behavior consistent in the other PR that's semver-major.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LiviaMedeiros LiviaMedeiros Apr 24, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yep! Can we have one more CI run please? I wanna make sure that testing this part for success and continuing on won't show anything unexpected for these platforms.

Also, results on AIX are bothering: https://ci.nodejs.org/job/node-test-commit-aix/40804/nodes=aix72-ppc64/testReport/junit/(root)/test/parallel_test_fs_read_position_validation/
This one reported Error: EFBIG: file too large, read on the test with Number.MAX_SAFE_INTEGER
I guess there's no choice but to suppress this particular error in testValid.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@LiviaMedeiros LiviaMedeiros Apr 24, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks a lot.
If I'm reading CI reports correctly, the results were mutually exclusive, except for AIX.
Helper functions got slightly overgeneralized but it should be fine.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aduh95 aduh95 added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Apr 23, 2022
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Apr 23, 2022
@nodejs-github-bot

nodejs-github-bot commented Apr 23, 2022

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/43655/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Apr 24, 2022
CI Reliability 2022-04-24 nodejs/reliability#261
Open
21 tasks
@aduh95 aduh95 added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Apr 24, 2022
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Apr 24, 2022
@nodejs-github-bot

nodejs-github-bot commented Apr 24, 2022

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/43662/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Apr 24, 2022

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/43665/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@LiviaMedeiros LiviaMedeiros changed the title fs: replace EINVAL with exception when position + length exceeds int64 limit test: add test for position validation in fs.read() and fs.readSync() Apr 24, 2022
aduh95
aduh95 approved these changes Apr 24, 2022
View reviewed changes

@aduh95 aduh95 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Assuming the CI results are green, that'd be great If you could rebase and update the commit message (otherwise it can be done by whoever lands this PR)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LiviaMedeiros reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/parallel/test-fs-read-position-validation.mjs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@LiviaMedeiros @aduh95
test: add test for position validation in fs.read() and fs.readSync()
c9054a5
Co-authored-by: Antoine du Hamel <[email protected]>
@LiviaMedeiros
LiviaMedeiros force-pushed the fs-reads-position-einvalfix branch from 18700ee to c9054a5 Compare April 24, 2022 13:32
@aduh95 aduh95 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Apr 24, 2022
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Apr 24, 2022
@nodejs-github-bot

nodejs-github-bot commented Apr 24, 2022

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/43666/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Apr 25, 2022
CI Reliability 2022-04-25 nodejs/reliability#262
Open
CI Reliability 2022-04-26 nodejs/reliability#263
Open
CI Reliability 2022-04-27 nodejs/reliability#264
Open
@aduh95 aduh95 mentioned this pull request Apr 27, 2022
fs: make params in writing methods optional #42601
Merged
This was referenced Apr 28, 2022
CI Reliability 2022-04-28 nodejs/reliability#265
Open
CI Reliability 2022-04-29 nodejs/reliability#266
Open
aduh95
aduh95 approved these changes May 2, 2022
View reviewed changes
@aduh95 aduh95 added the commit-queue PRs queued for automated landing through the Commit Queue. label May 2, 2022
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label May 2, 2022
@nodejs-github-bot nodejs-github-bot added the commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. label May 2, 2022
@nodejs-github-bot

nodejs-github-bot commented May 2, 2022

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/42837
✔  Done loading data for nodejs/node/pull/42837
----------------------------------- PR info ------------------------------------
Title      test: add test for `position` validation in `fs.read()` and `fs.readSync()` (#42837)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     LiviaMedeiros:fs-reads-position-einvalfix -> nodejs:master
Labels     fs, author ready, needs-ci
Commits    1
 - test: add test for position validation in fs.read() and fs.readSync()
Committers 1
 - LiviaMedeiros 
PR-URL: https://github.com/nodejs/node/pull/42837
Reviewed-By: Antoine du Hamel 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/42837
Reviewed-By: Antoine du Hamel 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sat, 23 Apr 2022 11:49:14 GMT
   ✔  Approvals: 1
   ✔  - Antoine du Hamel (@aduh95) (TSC): https://github.com/nodejs/node/pull/42837#pullrequestreview-959354525
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2022-04-24T13:53:02Z: https://ci.nodejs.org/job/node-test-pull-request/43666/
- Querying data for job/node-test-pull-request/43666/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/master up to date...
From https://github.com/nodejs/node
 * branch                  master     -> FETCH_HEAD
   acffd3d9e6..916a13a8a3  master     -> origin/master
✔  origin/master is now up-to-date
master is out of sync with origin/master. Mismatched commits:
 - 668380b721 http2: compat support for array headers
 - 916a13a8a3 http2: compat support for array headers
--------------------------------------------------------------------------------
HEAD is now at 916a13a8a3 http2: compat support for array headers
   ✔  Reset to origin/master
- Downloading patch for 42837
From https://github.com/nodejs/node
 * branch                  refs/pull/42837/merge -> FETCH_HEAD
✔  Fetched commits as 916a13a8a36d..c9054a554940
--------------------------------------------------------------------------------
[master 548016233f] test: add test for position validation in fs.read() and fs.readSync()
 Author: LiviaMedeiros 
 Date: Sat Apr 23 19:06:45 2022 +0800
 1 file changed, 133 insertions(+)
 create mode 100644 test/parallel/test-fs-read-position-validation.mjs
   ✔  Patches applied
--------------------------------------------------------------------------------
--------------------------------- New Message ----------------------------------
test: add test for position validation in fs.read() and fs.readSync()

Co-authored-by: Antoine du Hamel [email protected]

PR-URL: #42837
Reviewed-By: Antoine du Hamel [email protected]

[master a5d9fd2e47] test: add test for position validation in fs.read() and fs.readSync()
Author: LiviaMedeiros [email protected]
Date: Sat Apr 23 19:06:45 2022 +0800
1 file changed, 133 insertions(+)
create mode 100644 test/parallel/test-fs-read-position-validation.mjs
✖ a5d9fd2e475437aa0d7ffc5248a2d74f49a86cde
✖ 1:0 Co-authored-by must be a trailer co-authored-by-is-trailer
✔ 0:0 skipping fixes-url fixes-url
✔ 0:0 blank line after title line-after-title
✔ 0:0 line-lengths are valid line-length
✔ 0:0 metadata is at end of message metadata-end
✔ 3:8 PR-URL is valid. pr-url
✔ 0:0 reviewers are valid reviewers
✔ 0:0 valid subsystems subsystem
✔ 0:0 Title is formatted correctly. title-format
⚠ 0:50 Title should be <= 50 columns. title-length

ℹ Please fix the commit message and try again.

https://github.com/nodejs/node/actions/runs/2259461302

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aduh95
aduh95 merged commit 6be94c9 into nodejs:master May 2, 2022
@aduh95

aduh95 commented May 2, 2022

Copy link
Copy Markdown
Contributor

Landed in 6be94c9

LiviaMedeiros reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

RafaelGSS pushed a commit that referenced this pull request May 10, 2022
@LiviaMedeiros @aduh95 @RafaelGSS
test: add test for position validation in fs.read() and fs.readSync()
68ed3c8
PR-URL: #42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
@RafaelGSS RafaelGSS mentioned this pull request May 10, 2022
v18.2.0 proposal #43036
Merged
juanarbol pushed a commit that referenced this pull request May 31, 2022
@LiviaMedeiros @aduh95 @juanarbol
test: add test for position validation in fs.read() and fs.readSync()
51a6afa
PR-URL: #42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
@juanarbol juanarbol mentioned this pull request May 31, 2022
2022-05-XX, Version 16.16.0 'Gallium' (LTS) #43268
Closed
@LiviaMedeiros LiviaMedeiros mentioned this pull request Jun 27, 2022
[v16.x backport] test: add test for position validation in fs.read() and fs.readSync() #43588
Closed
LiviaMedeiros added a commit to LiviaMedeiros/node that referenced this pull request Jun 27, 2022
@LiviaMedeiros @aduh95
test: add test for position validation in fs.read() and fs.readSync()
1656e1b
PR-URL: nodejs#42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Backport-PR-URL: nodejs#43588
danielleadams pushed a commit that referenced this pull request Jun 27, 2022
@LiviaMedeiros @aduh95 @danielleadams
test: add test for position validation in fs.read() and fs.readSync()
0ce9a6c
PR-URL: #42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
targos pushed a commit that referenced this pull request Jul 12, 2022
@LiviaMedeiros @aduh95 @targos
test: add test for position validation in fs.read() and fs.readSync()
e55a3ca
PR-URL: #42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
targos pushed a commit that referenced this pull request Jul 31, 2022
@LiviaMedeiros @aduh95 @targos
test: add test for position validation in fs.read() and fs.readSync()
eceb318
PR-URL: #42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
@targos targos mentioned this pull request Aug 3, 2022
v16.17.0 release proposal #44098
Merged
guangwong pushed a commit to noslate-project/node that referenced this pull request Oct 10, 2022
@LiviaMedeiros @aduh95 @guangwong
test: add test for position validation in fs.read() and fs.readSync()
73f39bf
PR-URL: nodejs/node#42837
Co-authored-by: Antoine du Hamel <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@aduh95 aduh95 aduh95 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@LiviaMedeiros @nodejs-github-bot @aduh95
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.