Skip to content

Commit

Permalink
buffer: throw when writing beyond buffer"
Browse files Browse the repository at this point in the history
This reverts commit dd8eeec.

PR-URL: nodejs#54588
Refs: nodejs#54524
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
ronag authored and jasnell committed Sep 28, 2024
1 parent e1d8b4f commit 3800d60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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(() => {
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',
}));

0 comments on commit 3800d60

Please sign in to comment.