-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
fs.readFileSync returns incorrect buffer when target file is very small. #35351
Comments
I see this on latest node on git:master ❯ echo "console.log(require('fs').readFileSync('file.bin').buffer)" | out/Release/node
ArrayBuffer {
[Uint8Contents]: <2f 00 00 00 00 00 00 00 2f 00 00 00 00 00 00 00 01 02 03 04 01 02 03 04 01 02 03 04 01 02 03 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 8092 more bytes>,
byteLength: 8192
} However, are you sure that you're calling what you expect? Given that echo '01020304010203040102030401020304' | xxd -r -p > file.bin
echo "console.log(require('fs').readFileSync('file.bin'))" | node correctly produces: |
This is likely working as intended. It's using the buffer pool to improve the overall performance. It's not really intended to access the underlying buffer. We could use |
Thanks for the context, that makes sense. Let me clarify my use case a bit to shine some light on why I thought this might be a bug. I didn't write out the expected value correctly, I do want an ArrayBuffer, not a Buffer. The reason I'm grabbing the underlying buffer is because I have some code that's shared between client and server that consumes ArrayBuffers, and on the server I want to get the file contents in to an ArrayBuffer to pass to this code. So maybe it's working as expected, which is fine, but it's still a bit surprising. Given that there is a use case for wanting to get the data as an ArrayBuffer, it seems like just getting |
The notes in b = fs.readFileSync(filename)
b = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength) I'll close this out but please open a pull request if you have suggestions for improvements. |
Sounds good, thanks! I guess I should have checked the documentation more carefully :) |
What steps will reproduce the bug?
Create a small binary file, and then attempt to read it using
fs.readFileSync
. Here's a couple quick shell lines to reproduce the issue:The expected output is of course something like 01 02 03 04 01 02 03 04 ... etc. But instead you see a buffer that's 8192 bytes long and contains some other junk like loaded source file contents. The binary file appears to be loaded somewhere in that 8k block, but it's not at the beginning, and it's not just that file.
How often does it reproduce? Is there a required condition?
Reproduces every time I try. It only happens with files smaller than a few kB. I haven't narrowed it down to the exact threshold size.
What is the expected behavior?
The file is loaded correctly and this is printed:
What do you see instead?
Random junk around that file. i.e.
Additional information
This is not an issue with
readFile
onlyreadFileSync
. With the same test filereadFile
seems to work fine.The text was updated successfully, but these errors were encountered: