Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 2cad7a6

Browse files
committed
buffer: throw when writing beyond buffer
Previously one could write anywhere in a buffer pool if they accidently got their offset wrong. Mainly because the cc level checks only test against the parent slow buffer and not against the js object properties. So now we check to make sure values won't go beyond bounds without letting the dev know.
1 parent 3a2b503 commit 2cad7a6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/buffer.js

+3
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
339339
}
340340
encoding = String(encoding || 'utf8').toLowerCase();
341341

342+
if (string.length > 0 && (length < 0 || offset < 0))
343+
throw new RangeError('attempt to write beyond buffer bounds');
344+
342345
var ret;
343346
switch (encoding) {
344347
case 'hex':

test/simple/test-buffer.js

+10
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ new Buffer(0);
221221
b.write('', 1024);
222222
b.write('', 2048);
223223

224+
// throw when writing past bounds from the pool
225+
assert.throws(function() {
226+
b.write('a', 2048);
227+
}, RangeError);
228+
229+
// throw when writing to negative offset
230+
assert.throws(function() {
231+
b.write('a', -1);
232+
}, RangeError);
233+
224234
// try to copy 0 bytes worth of data into an empty buffer
225235
b.copy(new Buffer(0), 0, 0, 0);
226236

0 commit comments

Comments
 (0)