Skip to content

Commit 2400cbc

Browse files
committedApr 16, 2019
fs: fix infinite loop with async recursive mkdir on Windows
If `file` is a file then on Windows `mkdir` on `file/a` returns an `ENOENT` error while on POSIX the equivalent returns `ENOTDIR`. On the POSIX systems `ENOTDIR` would break out of the loop but on Windows the `ENOENT` would strip off the `a` and attempt to make `file` as a directory. This would return `EEXIST` but the code wasn't detecting that the existing path was a file and attempted to make `file/a` again. PR-URL: #27207 Fixes: #27198 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Ben Coe <[email protected]>
1 parent 96e46d3 commit 2400cbc

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed
 

‎src/node_file.cc

+32-16
Original file line numberDiff line numberDiff line change
@@ -1270,8 +1270,15 @@ int MKDirpSync(uv_loop_t* loop,
12701270
}
12711271
default:
12721272
uv_fs_req_cleanup(req);
1273+
int orig_err = err;
12731274
err = uv_fs_stat(loop, req, next_path.c_str(), nullptr);
1274-
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) return UV_EEXIST;
1275+
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) {
1276+
uv_fs_req_cleanup(req);
1277+
if (orig_err == UV_EEXIST && continuation_data.paths.size() > 0) {
1278+
return UV_ENOTDIR;
1279+
}
1280+
return UV_EEXIST;
1281+
}
12751282
if (err < 0) return err;
12761283
break;
12771284
}
@@ -1338,23 +1345,32 @@ int MKDirpAsync(uv_loop_t* loop,
13381345
break;
13391346
}
13401347
default:
1341-
if (err == UV_EEXIST &&
1342-
req_wrap->continuation_data->paths.size() > 0) {
1343-
uv_fs_req_cleanup(req);
1344-
MKDirpAsync(loop, req, path.c_str(),
1345-
req_wrap->continuation_data->mode, nullptr);
1346-
} else {
1348+
uv_fs_req_cleanup(req);
1349+
// Stash err for use in the callback.
1350+
req->data = reinterpret_cast<void*>(static_cast<intptr_t>(err));
1351+
int err = uv_fs_stat(loop, req, path.c_str(),
1352+
uv_fs_callback_t{[](uv_fs_t* req) {
1353+
FSReqBase* req_wrap = FSReqBase::from_req(req);
1354+
int err = req->result;
1355+
if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST &&
1356+
req_wrap->continuation_data->paths.size() > 0) {
1357+
if (err == 0 && S_ISDIR(req->statbuf.st_mode)) {
1358+
Environment* env = req_wrap->env();
1359+
uv_loop_t* loop = env->event_loop();
1360+
std::string path = req->path;
1361+
uv_fs_req_cleanup(req);
1362+
MKDirpAsync(loop, req, path.c_str(),
1363+
req_wrap->continuation_data->mode, nullptr);
1364+
return;
1365+
}
1366+
err = UV_ENOTDIR;
1367+
}
13471368
// verify that the path pointed to is actually a directory.
1369+
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST;
13481370
uv_fs_req_cleanup(req);
1349-
int err = uv_fs_stat(loop, req, path.c_str(),
1350-
uv_fs_callback_t{[](uv_fs_t* req) {
1351-
FSReqBase* req_wrap = FSReqBase::from_req(req);
1352-
int err = req->result;
1353-
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST;
1354-
req_wrap->continuation_data->Done(err);
1355-
}});
1356-
if (err < 0) req_wrap->continuation_data->Done(err);
1357-
}
1371+
req_wrap->continuation_data->Done(err);
1372+
}});
1373+
if (err < 0) req_wrap->continuation_data->Done(err);
13581374
break;
13591375
}
13601376
break;

‎test/parallel/test-fs-mkdir.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ function nextdir() {
132132
}
133133
}
134134

135+
// mkdirpSync when part of the path is a file.
136+
{
137+
const filename = path.join(tmpdir.path, nextdir(), nextdir());
138+
const pathname = path.join(filename, nextdir(), nextdir());
139+
140+
fs.mkdirSync(path.dirname(filename));
141+
fs.writeFileSync(filename, '', 'utf8');
142+
143+
try {
144+
fs.mkdirSync(pathname, { recursive: true });
145+
throw new Error('unreachable');
146+
} catch (err) {
147+
assert.notStrictEqual(err.message, 'unreachable');
148+
assert.strictEqual(err.code, 'ENOTDIR');
149+
assert.strictEqual(err.syscall, 'mkdir');
150+
}
151+
}
152+
135153
// `mkdirp` when folder does not yet exist.
136154
{
137155
const pathname = path.join(tmpdir.path, nextdir(), nextdir());
@@ -149,11 +167,25 @@ function nextdir() {
149167

150168
fs.mkdirSync(path.dirname(pathname));
151169
fs.writeFileSync(pathname, '', 'utf8');
152-
fs.mkdir(pathname, { recursive: true }, (err) => {
170+
fs.mkdir(pathname, { recursive: true }, common.mustCall((err) => {
153171
assert.strictEqual(err.code, 'EEXIST');
154172
assert.strictEqual(err.syscall, 'mkdir');
155173
assert.strictEqual(fs.statSync(pathname).isDirectory(), false);
156-
});
174+
}));
175+
}
176+
177+
// `mkdirp` when part of the path is a file.
178+
{
179+
const filename = path.join(tmpdir.path, nextdir(), nextdir());
180+
const pathname = path.join(filename, nextdir(), nextdir());
181+
182+
fs.mkdirSync(path.dirname(filename));
183+
fs.writeFileSync(filename, '', 'utf8');
184+
fs.mkdir(pathname, { recursive: true }, common.mustCall((err) => {
185+
assert.strictEqual(err.code, 'ENOTDIR');
186+
assert.strictEqual(err.syscall, 'mkdir');
187+
assert.strictEqual(fs.existsSync(pathname), false);
188+
}));
157189
}
158190

159191
// mkdirpSync dirname loop

‎test/parallel/test-fs-promises.js

+16
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ async function getHandle(dest) {
309309
}
310310
}
311311

312+
// `mkdirp` when part of the path is a file.
313+
{
314+
const file = path.join(tmpDir, nextdir(), nextdir());
315+
const dir = path.join(file, nextdir(), nextdir());
316+
await mkdir(path.dirname(file));
317+
await writeFile(file);
318+
try {
319+
await mkdir(dir, { recursive: true });
320+
throw new Error('unreachable');
321+
} catch (err) {
322+
assert.notStrictEqual(err.message, 'unreachable');
323+
assert.strictEqual(err.code, 'ENOTDIR');
324+
assert.strictEqual(err.syscall, 'mkdir');
325+
}
326+
}
327+
312328
// mkdirp ./
313329
{
314330
const dir = path.resolve(tmpDir, `${nextdir()}/./${nextdir()}`);

0 commit comments

Comments
 (0)
Please sign in to comment.