From 8f316859a51871ae2dea569f2aa60dd342a55e4b Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Thu, 28 Apr 2016 14:58:42 -0500 Subject: [PATCH] dir-reader: account for entries being 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. --- lib/dir-reader.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/dir-reader.js b/lib/dir-reader.js index 820cdc8..b220658 100644 --- a/lib/dir-reader.js +++ b/lib/dir-reader.js @@ -24,6 +24,7 @@ function DirReader (props) { } self.entries = null + self._entries = [] self._index = -1 self._paused = false self._length = -1 @@ -47,6 +48,7 @@ DirReader.prototype._getEntries = function () { if (er) return self.error(er) self.entries = entries + self._entries = entries.slice() self.emit('entries', entries) if (self._paused) self.once('resume', processEntries) @@ -74,7 +76,7 @@ DirReader.prototype._read = function () { } self._index++ - if (self._index >= self.entries.length) { + if (self._index >= self._entries.length) { if (!self._ended) { self._ended = true self.emit('end') @@ -83,12 +85,14 @@ DirReader.prototype._read = function () { return } - // ok, handle this one, then. - // save creating a proxy, by stat'ing the thing now. - var p = path.resolve(self._path, self.entries[self._index]) + var nextEntry = self._entries[self._index] + if (!nextEntry) return this._read() + + // ok, handle this one, then. + var p = path.resolve(self._path, nextEntry) assert(p !== self._path) - assert(self.entries[self._index]) + assert(nextEntry) // set this to prevent trying to _read() again in the stat time. self._currentEntry = p