bl 3.0.0 → 3.0.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
@@ -185,18 +185,22 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
185
185
 
186
186
  if (bytes > l) {
187
187
  this._bufs[i].copy(dst, bufoff, start)
188
+ bufoff += l
188
189
  } else {
189
190
  this._bufs[i].copy(dst, bufoff, start, start + bytes)
191
+ bufoff += l
190
192
  break
191
193
  }
192
194
 
193
- bufoff += l
194
195
  bytes -= l
195
196
 
196
197
  if (start)
197
198
  start = 0
198
199
  }
199
200
 
201
+ // safeguard so that we don't return uninitialized memory
202
+ if (dst.length > bufoff) return dst.slice(0, bufoff)
203
+
200
204
  return dst
201
205
  }
202
206
 
@@ -232,6 +236,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
232
236
  }
233
237
 
234
238
  BufferList.prototype.consume = function consume (bytes) {
239
+ // first, normalize the argument, in accordance with how Buffer does it
240
+ bytes = Math.trunc(bytes)
241
+ // do nothing if not a positive number
242
+ if (Number.isNaN(bytes) || bytes <= 0) return this
243
+
235
244
  while (this._bufs.length) {
236
245
  if (bytes >= this._bufs[0].length) {
237
246
  bytes -= this._bufs[0].length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "3.0.0",
3
+ "version": "3.0.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')