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

fs: fix edge case in readFileSync utf8 fast path #52101

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
if (CheckOpenPermissions(env, path, flags).IsNothing()) return;

FS_SYNC_TRACE_BEGIN(open);
file = uv_fs_open(nullptr, &req, *path, flags, O_RDONLY, nullptr);
file = uv_fs_open(nullptr, &req, *path, flags, 0666, nullptr);
FS_SYNC_TRACE_END(open);
if (req.result < 0) {
uv_fs_req_cleanup(&req);
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-fs-read-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,37 @@ require('../common');
const assert = require('assert');
const fs = require('fs');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');

const fn = fixtures.path('elipses.txt');
tmpdir.refresh();

const s = fs.readFileSync(fn, 'utf8');
for (let i = 0; i < s.length; i++) {
assert.strictEqual(s[i], '\u2026');
}
assert.strictEqual(s.length, 10000);

// Test file permissions set for readFileSync() in append mode.
{
const expectedMode = 0o666 & ~process.umask();

for (const test of [
{ },
{ encoding: 'ascii' },
{ encoding: 'base64' },
{ encoding: 'hex' },
{ encoding: 'latin1' },
{ encoding: 'uTf8' }, // case variation
{ encoding: 'utf16le' },
{ encoding: 'utf8' },
]) {
const opts = { ...test, flag: 'a+' };
const file = tmpdir.resolve(`testReadFileSyncAppend${opts.encoding ?? ''}.txt`);
const variant = `for '${file}'`;

const content = fs.readFileSync(file, opts);
assert.strictEqual(opts.encoding ? content : content.toString(), '', `file contents ${variant}`);
assert.strictEqual(fs.statSync(file).mode & 0o777, expectedMode, `file permissions ${variant}`);
}
}
Loading