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: use byteLength to handle ArrayBuffer views #38187

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ function read(fd, buffer, offset, length, position, callback) {
({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.length,
length = buffer.byteLength,
position
} = options);
}
Expand Down Expand Up @@ -586,15 +586,15 @@ ObjectDefineProperty(read, internalUtil.customPromisifyArgs,
function readSync(fd, buffer, offset, length, position) {
fd = getValidatedFd(fd);

validateBuffer(buffer);

if (arguments.length <= 3) {
// Assume fs.read(fd, buffer, options)
const options = offset || {};

({ offset = 0, length = buffer.length, position } = options);
({ offset = 0, length = buffer.byteLength, position } = options);
}

validateBuffer(buffer);

if (offset == null) {
offset = 0;
} else {
Expand Down Expand Up @@ -681,7 +681,7 @@ function write(fd, buffer, offset, length, position, callback) {
validateInteger(offset, 'offset');
}
if (typeof length !== 'number')
length = buffer.length - offset;
length = buffer.byteLength - offset;
if (typeof position !== 'number')
position = null;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ tmpdir.refresh();
fs.closeSync(fd);
}));
}

// fs.write with a DataView, without the offset and length parameters:
{
const filename = path.join(tmpdir.path, 'write8.txt');
fs.open(filename, 'w', 0o644, common.mustSucceed((fd) => {
const cb = common.mustSucceed((written) => {
assert.strictEqual(written, expected.length);
fs.closeSync(fd);

const found = fs.readFileSync(filename, 'utf8');
assert.strictEqual(found, expected.toString());
});

const uint8 = Uint8Array.from(expected);
fs.write(fd, new DataView(uint8.buffer), cb);
}));
}