Skip to content
Closed
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
buffer: throw when writing beyond buffer"
This reverts commit dd8eeec.

PR-URL: #54588
Refs: #54524
  • Loading branch information
ronag committed Aug 27, 2024
commit 2edea24735731f0f4fa230629a33acafa15890ab
12 changes: 6 additions & 6 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,33 +1036,33 @@ function addBufferPrototypeMethods(proto) {
proto.hexSlice = hexSlice;
proto.ucs2Slice = ucs2Slice;
proto.utf8Slice = utf8Slice;
proto.asciiWrite = function(string, offset = 0, length = this.byteLength) {
proto.asciiWrite = function(string, offset = 0, length = this.byteLength - offset) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return asciiWriteStatic(this, string, offset, length);
};
proto.base64Write = base64Write;
proto.base64urlWrite = base64urlWrite;
proto.latin1Write = function(string, offset = 0, length = this.byteLength) {
proto.latin1Write = function(string, offset = 0, length = this.byteLength - offset) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return latin1WriteStatic(this, string, offset, length);
};
proto.hexWrite = hexWrite;
proto.ucs2Write = ucs2Write;
proto.utf8Write = function(string, offset = 0, length = this.byteLength) {
proto.utf8Write = function(string, offset = 0, length = this.byteLength - offset) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
if (length < 0 || length > this.byteLength - offset) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return utf8WriteStatic(this, string, offset, length);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,16 @@ assert.throws(() => {
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
}));


assert.throws(() => {
Comment thread
jasnell marked this conversation as resolved.
Buffer.alloc(1).asciiWrite('ww', 0, 2);
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
}));

assert.throws(() => {
Buffer.alloc(1).asciiWrite('ww', 1, 1);
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
}));