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

filehandle.read(buffer) can't read file when options are omitted #47183

Closed
H1Gdev opened this issue Mar 21, 2023 · 3 comments · Fixed by #51087
Closed

filehandle.read(buffer) can't read file when options are omitted #47183

H1Gdev opened this issue Mar 21, 2023 · 3 comments · Fixed by #51087
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@H1Gdev
Copy link
Contributor

H1Gdev commented Mar 21, 2023

Version

v18.12.1

Platform

Linux ubuntu-focal 5.4.0-144-generic #161-Ubuntu SMP Fri Feb 3 14:49:04 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

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

async function test() {
  let path = '/path/to/file';

  let buf = Buffer.alloc(4);
  let handle;
  try {
    handle = await fs.open(path);
    {
      let buf = Buffer.alloc(4);
      let { bytesRead, buffer } = await handle.read(buf);
      //let { bytesRead, buffer } = await handle.read(buf, {}); // It is behavior I would expect.
      console.log(bytesRead);
      console.log(buffer);
      console.log(buf);
    }
  } finally {
    await handle?.close();
  }
}
test();

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

always

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

https://nodejs.org/dist/latest-v18.x/docs/api/fs.html#filehandlereadbuffer-options

Even if you omit options, it will be default values.

What do you see instead?

0
<Buffer 00 00 00 00>
<Buffer 00 00 00 00>

Additional information

No response

@H1Gdev
Copy link
Contributor Author

H1Gdev commented Mar 21, 2023

if (offset == null) {
offset = 0;
} else {

It looks like the length is still zero here.
Adding the following might work as expected.

length = buffer.byteLength - offset

@deokjinkim deokjinkim added the fs Issues and PRs related to the fs subsystem / file system. label Mar 21, 2023
@deokjinkim deokjinkim changed the title Can not read file. filehandle.read(buffer) can't read file when options are ommitted Mar 21, 2023
@mscdex mscdex changed the title filehandle.read(buffer) can't read file when options are ommitted filehandle.read(buffer) can't read file when options are omitted Mar 21, 2023
@pulkit-30
Copy link
Contributor

is this issue valid? please confirm.

nodejs-github-bot pushed a commit that referenced this issue Dec 22, 2023
PR-URL: #51087
Fixes: #47183
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Jan 2, 2024
PR-URL: #51087
Fixes: #47183
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@markrian
Copy link

markrian commented Jan 2, 2024

Edit: Oh, I see #51087 probably fixes this. Sorry for the noise!


@pulkit-30 I can still reproduce this on node 21.5.0 and 20.10.0 with this script:

import { open } from 'fs/promises';

async function main() {
  const bytes = new Uint8Array(8);
  const fileHandle = await open(process.argv[2]);
  console.debug(await fileHandle.read(bytes));
}

main();

Invoked like this:

node index.mjs some-file.txt

Produces this output:

[Object: null prototype] {
  bytesRead: 0,
  buffer: Uint8Array(8) [
    0, 0, 0, 0,
    0, 0, 0, 0
  ]
}

richardlau pushed a commit that referenced this issue Mar 25, 2024
PR-URL: #51087
Fixes: #47183
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
4 participants