Skip to content

[fs/promises] FileHandle.read does not respect reading from position=0 #40715

Closed
@mihilmy

Description

@mihilmy

Version

v14.18.1

Platform

Linux 5.4.141-78.230.amzn2int.x86_64 Fri Aug 27 01:18:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Code

const fs = require('fs/promises');

let handle;

async function main() {
  handle = await fs.open(__filename, 'r');
  await read(0, 5);  // Should get const ✅
  await read(11, 7); // Should get require  ✅
  await read(0, 5);  // Should get const again; zero is not honored ❌ 
  await read(11, 7); // Should get require ✅
}

async function read(start, length) {
  const buffer = Buffer.alloc(length);
  await handle.read({ buffer, position: start, length });
  console.log(buffer.toString());
}

Console Output

const
require
 fs =
require

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

It's reproduced whenever you try to use 0 as the seek position

What is the expected behavior?

I would expect to be able to navigate back and forth using different positions of the file..

What do you see instead?

I am able to do that only if the position > 0 (STRICTLY greater than)

Additional information

I believe the cause might be this line here:

position = bufferOrOptions.position || null;

Since 0 is falsy in javascript this might lead to this behavior. I am happy to put in a pull request to fix if indeed this is the cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions