Skip to content

fs.readSync does not validate invalid position type (accepts object instead of throwing) #62638

Description

@kevgeoleo

Version

v24.13.1

Platform

Linux KContainer 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1 (2022-03-07) x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Hi,

I would like to report a behavior in Node that can be reproduced by running the below snippet

"use strict"
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

const memPath = fs.mkdtempSync(path.join(os.tmpdir(), 'node-temp-'));
const memFile = path.join(memPath, 'tempfile');
fs.writeFileSync(memFile, '');

// Open the file for reading and writing
const fd = fs.openSync(memFile, 'r+');

const ankor = new TextEncoder().encode('');

// This call is intentionally incorrect: `position` should be a number or null.
try {
  fs.readSync(fd, ankor, 0, ankor.length, { not: 'a number' } /* invalid position */);
  console.log('fs.readSync did not throw as expected');
} catch (err) {
  console.log('fs.readSync threw an error as expected:', err instanceof TypeError ? 'TypeError' : err.name, '-', err.message);
}

fs.closeSync(fd);

How often does it reproduce? Is there a required condition?

No special condition for reproduction

What is the expected behavior? Why is that the expected behavior?

Position argument must be a number and error should be thrown
According to the documentation it should be number: https://nodejs.org/api/fs.html#fsreadsyncfd-buffer-offset-length-position

root@KContainer:~/14128# deno run -A poc.mjs
fs.readSync threw an error as expected: TypeError - The "position" argument must be of type bigint or integer. Received an instance of Object

What do you see instead?

Does not throw even in strict mode

root@KContainer:~/14128# node poc.mjs
fs.readSync did not throw as expected

Additional information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions