Skip to content

Commit 08c6c8b

Browse files
ChALkeRlpinca
authored andcommitted
[fix] Ensure that concat() never returns uninitialized data (#1600)
This is just a safeguard that ensures that for cases if `totalLength` is by some mistake miscalculated, no uninitialized memory would be leaked.
1 parent 5b7315f commit 08c6c8b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/buffer-util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function concat(list, totalLength) {
2323
offset += buf.length;
2424
}
2525

26+
if (offset < totalLength) return target.slice(0, offset);
27+
2628
return target;
2729
}
2830

test/buffer-util.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
const { concat } = require('../lib/buffer-util');
6+
7+
describe('bufferUtil', () => {
8+
describe('concat', () => {
9+
it('never returns uninitialized data', () => {
10+
const buf = concat([Buffer.from([1, 2]), Buffer.from([3, 4])], 6);
11+
12+
assert.ok(buf.equals(Buffer.from([1, 2, 3, 4])));
13+
});
14+
});
15+
});

0 commit comments

Comments
 (0)