From 1b73ffe30bbd7feb974f002d43b34e018cf63a1c Mon Sep 17 00:00:00 2001 From: JP Richardson Date: Sun, 17 Apr 2016 01:47:08 -0500 Subject: [PATCH] (#93, #209) prevent stack overflow if bad dir on win32 --- lib/mkdirs/__tests__/README.md | 1 - lib/mkdirs/__tests__/issue-93.test.js | 36 +++++++++++++++++++++++++++ lib/mkdirs/mkdirs-sync.js | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) delete mode 100644 lib/mkdirs/__tests__/README.md create mode 100644 lib/mkdirs/__tests__/issue-93.test.js diff --git a/lib/mkdirs/__tests__/README.md b/lib/mkdirs/__tests__/README.md deleted file mode 100644 index a211103b..00000000 --- a/lib/mkdirs/__tests__/README.md +++ /dev/null @@ -1 +0,0 @@ -These tests imported from https://github.com/substack/node-mkdirp. \ No newline at end of file diff --git a/lib/mkdirs/__tests__/issue-93.test.js b/lib/mkdirs/__tests__/issue-93.test.js new file mode 100644 index 00000000..c6c33f77 --- /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..b4e4a9c1 100644 --- a/lib/mkdirs/mkdirs-sync.js +++ b/lib/mkdirs/mkdirs-sync.js @@ -24,6 +24,7 @@ function mkdirsSync (p, opts, made) { } catch (err0) { switch (err0.code) { case 'ENOENT' : + if (path.dirname(p) === p) throw err0 made = mkdirsSync(path.dirname(p), opts, made) mkdirsSync(p, opts, made) break