Skip to content

Commit

Permalink
fixup: throw on negative length
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 26, 2024
1 parent 9b0a26a commit 1dea557
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,9 @@ function addBufferPrototypeMethods(proto) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return asciiWriteStatic(this, string, offset, length);
};
proto.base64Write = base64Write;
Expand All @@ -1048,6 +1051,9 @@ function addBufferPrototypeMethods(proto) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return latin1WriteStatic(this, string, offset, length);
};
proto.hexWrite = hexWrite;
Expand All @@ -1056,6 +1062,9 @@ function addBufferPrototypeMethods(proto) {
if (offset < 0 || offset > this.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
}
if (length < 0) {
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
}
return utf8WriteStatic(this, string, offset, length);
};
}
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,11 @@ assert.strictEqual(Buffer.alloc(4)
assert.strictEqual(buf.write('ww'), 1);
assert.strictEqual(buf.toString(), 'w');
}

assert.throws(() =>

Check failure on line 116 in test/parallel/test-buffer-write.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

`assert.throws()` must be invoked with at least two arguments
{

Check failure on line 117 in test/parallel/test-buffer-write.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Opening curly brace does not appear on the same line as controlling statement
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));
});

0 comments on commit 1dea557

Please sign in to comment.