Skip to content

fs: fix error when writing buffers > INT32_MAX

Rodrigo Muino Tomonari requested to merge github/fork/zbjornson/36667-fixup into master

This reverts c380ee67. 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 (closed) 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

Merge request reports

Loading