Skip to content

Commit

Permalink
wrapped new header with try catch
Browse files Browse the repository at this point in the history
more aesthetic return

PR-URL: #229
Credit: @motty1985
Close: #229
Reviewed-by: @isaacs

EDIT(@isaacs): made it a warning (to match v5 behavior), and added test.
  • Loading branch information
mottymilshtein authored and isaacs committed Sep 17, 2019
1 parent 84ab44d commit 4a9069a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ module.exports = warner(class Parser extends EE {
}

[CONSUMEHEADER] (chunk, position) {
const header = new Header(chunk, position, this[EX], this[GEX])
let header
try {
header = new Header(chunk, position, this[EX], this[GEX])
} catch (er) {
return this.warn('invalid entry', er)
}

if (header.nullBlock)
this[EMIT]('nullBlock')
Expand Down
24 changes: 24 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,27 @@ t.test('end while consuming', t => {
mp.end(data)
mp.pipe(p)
})

t.test('header that throws', t => {
const expect = { message: 'invalid base256 encoding' }
const p = new Parse()
p.on('warn', (m, d) => {
t.match(d, expect)
t.end()
})
const h = new Header({
path: 'path',
mode: 0o07777, // gonna make this one invalid
uid: 1234,
gid: 4321,
size: 99,
type: 'File',
size: 1,
})
h.encode()
const buf = h.block
const bad = Buffer.from([0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
bad.copy(buf, 100)
t.throws(() => new Header(buf), expect, 'the header with that buffer throws')
p.write(buf)
})

0 comments on commit 4a9069a

Please sign in to comment.