Skip to content

Commit 6c95323

Browse files
vhainisaacs
authored andcommitted
write-entry: handle shrinked file size than expected by lstat
if file expands after lstat, EOF error thrown. but if file shrinks after lstat, [READ] will called indefinitely. - added test - [ONREAD] should [CLOSE] fd before emit, same as on [READ]
1 parent 34bdf6b commit 6c95323

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/write-entry.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,17 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
231231
er.path = this.absolute
232232
er.syscall = 'read'
233233
er.code = 'EOF'
234-
this.emit('error', er)
234+
this[CLOSE](fd)
235+
return this.emit('error', er)
236+
}
237+
238+
if (bytesRead > remain) {
239+
const er = new Error('expected EOF')
240+
er.path = this.absolute
241+
er.syscall = 'read'
242+
er.code = 'EOF'
243+
this[CLOSE](fd)
244+
return this.emit('error', er)
235245
}
236246

237247
// null out the rest of the buffer, if we could fit the block padding

test/write-entry.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,25 @@ t.test('read invalid EOF', t => {
554554
})
555555
})
556556

557+
t.test('read overflow expectation', t => {
558+
t.tearDown(mutateFS.statMutate((er, st) => {
559+
if (st)
560+
st.size = 3
561+
}));
562+
const f = '512-bytes.txt'
563+
const expect = {
564+
message: 'expected EOF',
565+
path: path.resolve(files, f),
566+
syscall: 'read',
567+
code: 'EOF'
568+
}
569+
t.throws(_ => new WriteEntry.Sync(f, { cwd: files, maxReadSize: 2 }), expect)
570+
new WriteEntry(f, { cwd: files, maxReadSize: 2 }).on('error', er => {
571+
t.match(er, expect)
572+
t.end()
573+
})
574+
})
575+
557576
t.test('short reads', t => {
558577
t.tearDown(mutateFS.zenoRead())
559578
const cases = {

0 commit comments

Comments
 (0)