Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit a55ae72

Browse files
evanlucasothiym23
authored andcommitted
read: account for entries changed after _read
If this.entries is changed after _read() has been called, we will be out of sync and try to access an invalid index of this.entries. When the entry cannot be found, we emit end and close, which can drop files from reading. (At least partially) fixes npm/npm#5082. Credit: @evanlucas Reviewed-By: @othiym23 PR-URL: #50
1 parent 3258080 commit a55ae72

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/dir-reader.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function DirReader (props) {
2424
}
2525

2626
self.entries = null
27+
self._entries = []
2728
self._index = -1
2829
self._paused = false
2930
self._length = -1
@@ -47,6 +48,7 @@ DirReader.prototype._getEntries = function () {
4748
if (er) return self.error(er)
4849

4950
self.entries = entries
51+
self._entries = entries.slice()
5052

5153
self.emit('entries', entries)
5254
if (self._paused) self.once('resume', processEntries)
@@ -74,7 +76,7 @@ DirReader.prototype._read = function () {
7476
}
7577

7678
self._index++
77-
if (self._index >= self.entries.length) {
79+
if (self._index >= self._entries.length) {
7880
if (!self._ended) {
7981
self._ended = true
8082
self.emit('end')
@@ -83,12 +85,14 @@ DirReader.prototype._read = function () {
8385
return
8486
}
8587

86-
// ok, handle this one, then.
87-
8888
// save creating a proxy, by stat'ing the thing now.
89-
var p = path.resolve(self._path, self.entries[self._index])
89+
var nextEntry = self._entries[self._index]
90+
if (!nextEntry) return this._read()
91+
92+
// ok, handle this one, then.
93+
var p = path.resolve(self._path, nextEntry)
9094
assert(p !== self._path)
91-
assert(self.entries[self._index])
95+
assert(nextEntry)
9296

9397
// set this to prevent trying to _read() again in the stat time.
9498
self._currentEntry = p

0 commit comments

Comments
 (0)