buffer: throw when writing beyond buffer"

This reverts commit dd8eeec3f0.

PR-URL: https://github.com/nodejs/node/pull/54588/
Refs: https://github.com/nodejs/node/pull/54524
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Robert Nagy 2024-08-27 10:58:40 +02:00 committed by James M Snell
parent e1d8b4f038
commit 3800d60c66
2 changed files with 19 additions and 6 deletions

View File

@ -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);

View File

@ -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',
}));