Skip to content
Open
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
15 changes: 12 additions & 3 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,30 +959,39 @@ function writeFloatBackwards(val, offset = 0) {

class FastBuffer extends Uint8Array {}

function asciiWrite(buf, string, offset = 0, length = buf.byteLength - offset) {
function asciiWrite(buf, string, offset = 0, length) {
offset = Number(offset);
if (offset < 0 || offset > buf.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length === undefined) length = buf.byteLength - offset;
else length = Number(length);
if (length < 0 || length > buf.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return asciiWriteStatic(buf, string, offset, length);
}

function latin1Write(buf, string, offset = 0, length = buf.byteLength - offset) {
function latin1Write(buf, string, offset = 0, length) {
offset = Number(offset);
if (offset < 0 || offset > buf.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length === undefined) length = buf.byteLength - offset;
else length = Number(length);
if (length < 0 || length > buf.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return latin1WriteStatic(buf, string, offset, length);
}

function utf8Write(buf, string, offset = 0, length = buf.byteLength - offset) {
function utf8Write(buf, string, offset = 0, length) {
offset = Number(offset);
if (offset < 0 || offset > buf.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length === undefined) length = buf.byteLength - offset;
else length = Number(length);
if (length < 0 || length > buf.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
Expand Down
5 changes: 5 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,11 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
size_t max_length = 0;

THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &offset));
if (offset > ts_obj_length) {
return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS(
env, "\"offset\" is outside of buffer bounds");
}

THROW_AND_RETURN_IF_OOB(
ParseArrayIndex(env, args[3], ts_obj_length - offset, &max_length));

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,17 @@ assert.throws(() => {
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
}));

for (const method of ['asciiWrite', 'latin1Write', 'utf8Write']) {
let calls = 0;
const offset = {
valueOf() {
calls++;
return 2;
},
};
assert.throws(() => Buffer.alloc(1)[method]('ww', offset, 1), common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
}));
assert.strictEqual(calls, 1);
}
Loading