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

fs: fix error when writing buffers > INT32_MAX #38546

Closed
wants to merge 1 commit into from

Conversation

zbjornson
Copy link
Contributor

@zbjornson zbjornson commented May 5, 2021

This reverts c380ee6. uv_fs_write returns an int, so it is not possible to ask it to write more than INT32_MAX. Instead, validate 'length' is an int32 in JS to avoid the assertion failure.

I think #36643 should be reopened. It's not possible that the reverted commit fixed it.

Repro:

const fs = require("fs");
const INT32_MAX =  0x7FFFFFFF;
const x = Buffer.allocUnsafe(INT32_MAX + 1);
const fd = fs.openSync("test.dat", "w");
console.log(fs.writeSync(fd, x, 0));
Error: UNKNOWN: unknown error, write
    at Object.writeSync (node:fs:882:3)
    at Object.<anonymous> (/home/zbjornson/test.js:9:16)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -2147483648,
  syscall: 'write',
  code: 'UNKNOWN'
}

That errno -2147483648 is 2147483648 wrapped around.

I didn't add a test for this because the test would require allocating a 2 GB buffer.

cc @RaisinTen

@github-actions github-actions bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels May 5, 2021
@Ayase-252
Copy link
Member

Confirmed on master branch, it throws a confusing Error,

node:internal/fs/utils:343
    throw err;
    ^

Error: EINVAL: invalid argument, write
    at Object.writeSync (node:fs:882:3)
    at Object.<anonymous> (/Users/qingyudeng/projects/node/index.js:5:16)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -22,
  syscall: 'write',
  code: 'EINVAL'
}

@aduh95
Copy link
Contributor

aduh95 commented May 5, 2021

Please add a test, you can ask it to skip if there's not enough memory on the host:

if (!common.enoughTestMem)
common.skip('skipped due to memory requirements');

@zbjornson
Copy link
Contributor Author

@aduh95 test added.

Thanks for the prompt reviews.

This reverts c380ee6. uv_fs_write returns an
int, so it is not possible to ask it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid the assertion failure.
@nodejs-github-bot

This comment has been minimized.

@RaisinTen RaisinTen added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 8, 2021
@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented May 9, 2021

@jasnell
Copy link
Member

jasnell commented May 10, 2021

Landed in 8886b63

@jasnell jasnell closed this May 10, 2021
jasnell pushed a commit that referenced this pull request May 10, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
moander pushed a commit to moander/node that referenced this pull request May 11, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: nodejs#38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
moander pushed a commit to moander/node that referenced this pull request May 11, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: nodejs#38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request May 17, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request May 30, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request Jun 5, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request Jun 5, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request Jun 11, 2021
This reverts c380ee6.
uv_fs_write returns an int, so it is not possible to ask
it to write more than INT32_MAX.

Instead, validate 'length' is an int32 in JS to avoid
the assertion failure.

PR-URL: #38546
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@zbjornson zbjornson deleted the 36667-fixup branch September 20, 2021 04:19
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. c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants