Skip to content

test: add test for `position` validation in `fs.read()` and `fs.readSync()`

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

To be expanded in: https://github.com/nodejs/node/pull/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'
}

Merge request reports

Loading