From a0cb04b0b09fe572577f813bb1a729e242ea2e22 Mon Sep 17 00:00:00 2001 From: JP Richardson Date: Mon, 18 Apr 2016 03:52:19 -0500 Subject: [PATCH] (Closes #93) libs/mkdirs: prevent stack overflow if drive is not mounted in Windows --- lib/mkdirs/__tests__/issue-93.test.js | 36 +++++++++++++++++++++++++++ lib/mkdirs/mkdirs-sync.js | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/mkdirs/__tests__/issue-93.test.js diff --git a/lib/mkdirs/__tests__/issue-93.test.js b/lib/mkdirs/__tests__/issue-93.test.js new file mode 100644 index 00000000..f7fc1fb2 --- /dev/null +++ b/lib/mkdirs/__tests__/issue-93.test.js @@ -0,0 +1,36 @@ +var assert = require('assert') +var path = require('path') +var os = require('os') +var fse = require(process.cwd()) + +/* global before, describe, it */ + +describe('mkdirp: issue-93, win32, when drive does not exist, it should return a cleaner error', function () { + var TEST_DIR + + // only seems to be an issue on Windows. + if (process.platform !== 'win32') return + + before(function (done) { + TEST_DIR = path.join(os.tmpdir(), 'tests', 'fs-extra', 'mkdirp-issue-93') + fse.emptyDir(TEST_DIR, function (err) { + assert.ifError(err) + done() + }) + }) + + it('should return a cleaner error than inifinite loop, stack crash', function (done) { + var file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor + fse.mkdirp(file, function (err) { + assert.strictEqual(err.code, 'ENOENT') + + try { + fse.mkdirsSync(file) + } catch (err) { + assert.strictEqual(err.code, 'ENOENT') + } + + done() + }) + }) +}) diff --git a/lib/mkdirs/mkdirs-sync.js b/lib/mkdirs/mkdirs-sync.js index 2dc4f231..138d56ce 100644 --- a/lib/mkdirs/mkdirs-sync.js +++ b/lib/mkdirs/mkdirs-sync.js @@ -23,7 +23,8 @@ function mkdirsSync (p, opts, made) { made = made || p } catch (err0) { switch (err0.code) { - case 'ENOENT' : + case 'ENOENT': + if (path.dirname(p) === p) throw err0 made = mkdirsSync(path.dirname(p), opts, made) mkdirsSync(p, opts, made) break