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

@Ayase-252
Copy link
Member

@Ayase-252 Ayase-252 commented May 5, 2021

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 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');

jasnell
jasnell approved these changes May 5, 2021
@zbjornson
Copy link
Contributor Author

@zbjornson zbjornson commented May 5, 2021

@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.
jasnell
jasnell approved these changes May 7, 2021
@nodejs-github-bot

This comment has been hidden.

Trott
Trott approved these changes May 8, 2021
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented May 9, 2021

lpinca
lpinca approved these changes May 9, 2021
@jasnell
Copy link
Member

@jasnell jasnell commented May 10, 2021

Landed in 8886b63

@jasnell jasnell closed this May 10, 2021
jasnell added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
moander added a commit to moander/node that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
moander added a commit to moander/node that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos added a commit that referenced this issue 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 <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
@zbjornson zbjornson deleted the 36667-fixup branch Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants