Skip to content

Commit 0bd87ec

Browse files
mcollinarvagg
authored andcommitted
Fix unintialized memory access
Closes: #89
1 parent dc097f3 commit 0bd87ec

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

bl.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,22 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
173173

174174
if (bytes > l) {
175175
this._bufs[i].copy(dst, bufoff, start)
176+
bufoff += l
176177
} else {
177178
this._bufs[i].copy(dst, bufoff, start, start + bytes)
179+
bufoff += l
178180
break
179181
}
180182

181-
bufoff += l
182183
bytes -= l
183184

184185
if (start)
185186
start = 0
186187
}
187188

189+
// safeguard so that we don't return uninitialized memory
190+
if (dst.length > bufoff) return dst.slice(0, bufoff)
191+
188192
return dst
189193
}
190194

@@ -217,6 +221,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
217221
}
218222

219223
BufferList.prototype.consume = function consume (bytes) {
224+
// first, normalize the argument, in accordance with how Buffer does it
225+
bytes = Math.trunc(bytes)
226+
// do nothing if not a positive number
227+
if (Number.isNaN(bytes) || bytes <= 0) return this
228+
220229
while (this._bufs.length) {
221230
if (bytes >= this._bufs[0].length) {
222231
bytes -= this._bufs[0].length

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ tape('test toString encoding', function (t) {
381381
t.end()
382382
})
383383

384+
tape('uninitialized memory', function (t) {
385+
const secret = crypto.randomBytes(256)
386+
for (let i = 0; i < 1e6; i++) {
387+
const clone = Buffer.from(secret)
388+
const bl = new BufferList()
389+
bl.append(Buffer.from('a'))
390+
bl.consume(-1024)
391+
const buf = bl.slice(1)
392+
if (buf.indexOf(clone) !== -1) {
393+
t.fail(`Match (at ${i})`)
394+
break
395+
}
396+
}
397+
t.end()
398+
})
399+
384400
!process.browser && tape('test stream', function (t) {
385401
var random = crypto.randomBytes(65534)
386402
, rndhash = hash(random, 'md5')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy