diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index 20b22ad529385a..c547c1f993fb55 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -1040,7 +1040,7 @@ function addBufferPrototypeMethods(proto) { 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); @@ -1051,7 +1051,7 @@ function addBufferPrototypeMethods(proto) { 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); @@ -1062,7 +1062,7 @@ function addBufferPrototypeMethods(proto) { 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); diff --git a/test/parallel/test-buffer-write.js b/test/parallel/test-buffer-write.js index 309367c9c75ca1..128327c47e5d6f 100644 --- a/test/parallel/test-buffer-write.js +++ b/test/parallel/test-buffer-write.js @@ -106,18 +106,3 @@ assert.strictEqual(Buffer.alloc(4) assert.strictEqual(buf.write('ыы', 1, 'utf16le'), 4); assert.deepStrictEqual([...buf], [0, 0x4b, 0x04, 0x4b, 0x04, 0, 0, 0]); } - -{ - const buf = Buffer.alloc(1); - assert.strictEqual(buf.write('ww'), 1); - assert.strictEqual(buf.toString(), 'w'); -} - -assert.throws(() => { - const buf = Buffer.alloc(1); - assert.strictEqual(buf.asciiWrite('ww', 0, -1)); - assert.strictEqual(buf.latin1Write('ww', 0, -1)); - assert.strictEqual(buf.utf8Write('ww', 0, -1)); -}, common.expectsError({ - code: 'ERR_BUFFER_OUT_OF_BOUNDS', -}));