bl 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/bl.js +10 -1
  2. package/package.json +1 -1
  3. package/test/test.js +16 -0
package/bl.js CHANGED
@@ -186,18 +186,22 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
186
186
 
187
187
  if (bytes > l) {
188
188
  this._bufs[i].copy(dst, bufoff, start)
189
+ bufoff += l
189
190
  } else {
190
191
  this._bufs[i].copy(dst, bufoff, start, start + bytes)
192
+ bufoff += l
191
193
  break
192
194
  }
193
195
 
194
- bufoff += l
195
196
  bytes -= l
196
197
 
197
198
  if (start)
198
199
  start = 0
199
200
  }
200
201
 
202
+ // safeguard so that we don't return uninitialized memory
203
+ if (dst.length > bufoff) return dst.slice(0, bufoff)
204
+
201
205
  return dst
202
206
  }
203
207
 
@@ -233,6 +237,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
233
237
  }
234
238
 
235
239
  BufferList.prototype.consume = function consume (bytes) {
240
+ // first, normalize the argument, in accordance with how Buffer does it
241
+ bytes = Math.trunc(bytes)
242
+ // do nothing if not a positive number
243
+ if (Number.isNaN(bytes) || bytes <= 0) return this
244
+
236
245
  while (this._bufs.length) {
237
246
  if (bytes >= this._bufs[0].length) {
238
247
  bytes -= this._bufs[0].length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!",
5
5
  "main": "bl.js",
6
6
  "scripts": {
package/test/test.js CHANGED
@@ -431,6 +431,22 @@ tape('test toString encoding', function (t) {
431
431
  t.end()
432
432
  })
433
433
 
434
+ tape('uninitialized memory', function (t) {
435
+ const secret = crypto.randomBytes(256)
436
+ for (let i = 0; i < 1e6; i++) {
437
+ const clone = Buffer.from(secret)
438
+ const bl = new BufferList()
439
+ bl.append(Buffer.from('a'))
440
+ bl.consume(-1024)
441
+ const buf = bl.slice(1)
442
+ if (buf.indexOf(clone) !== -1) {
443
+ t.fail(`Match (at ${i})`)
444
+ break
445
+ }
446
+ }
447
+ t.end()
448
+ })
449
+
434
450
  !process.browser && tape('test stream', function (t) {
435
451
  var random = crypto.randomBytes(65534)
436
452
  , rndhash = hash(random, 'md5')