Skip to content

Commit 0d0512f

Browse files
committed
src: do not report an unread cwd in process.chdir() errors
When chdir() fails, the error includes the original working directory from uv_cwd(), but the result of uv_cwd() was not checked. If the working directory has been removed, uv_cwd() fails and leaves the buffer unwritten, and the error reported uninitialized stack bytes as its path. Leave the path out when uv_cwd() fails. Signed-off-by: Martin Mirchev <mirchevmartin2203@gmail.com>
1 parent 1724da8 commit 0d0512f

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/node_process_methods.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) {
9393
if (err) {
9494
// Also include the original working directory, since that will usually
9595
// be helpful information when debugging a `chdir()` failure.
96+
// uv_cwd() fails if the working directory was removed; buf is then not
97+
// written, so leave the original directory out of the error.
9698
char buf[PATH_MAX_BYTES];
9799
size_t cwd_len = sizeof(buf);
98-
uv_cwd(buf, &cwd_len);
99-
return env->ThrowUVException(err, "chdir", nullptr, buf, *path);
100+
const char* cwd = uv_cwd(buf, &cwd_len) == 0 ? buf : nullptr;
101+
return env->ThrowUVException(err, "chdir", nullptr, cwd, *path);
100102
}
101103
}
102104

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
// This tests the error from a failing process.chdir() when the current
4+
// working directory has been removed: the original directory cannot be
5+
// determined, so the error must not report one.
6+
const common = require('../common');
7+
const assert = require('assert');
8+
const { spawnSync } = require('child_process');
9+
const fs = require('fs');
10+
const { isMainThread } = require('worker_threads');
11+
12+
if (common.isWindows) {
13+
common.skip('the current working directory cannot be removed on Windows');
14+
}
15+
if (!isMainThread) {
16+
common.skip('process.chdir is not available in Workers');
17+
}
18+
19+
const tmpdir = require('../common/tmpdir');
20+
21+
tmpdir.refresh();
22+
const dir = tmpdir.resolve('removed-cwd');
23+
fs.mkdirSync(dir);
24+
const missing = tmpdir.resolve('missing');
25+
26+
const child = spawnSync(process.execPath, ['-e', `
27+
require('fs').rmdirSync(process.cwd());
28+
try {
29+
process.chdir(${JSON.stringify(missing)});
30+
} catch (err) {
31+
console.log(JSON.stringify({ code: err.code, path: err.path,
32+
dest: err.dest, message: err.message }));
33+
}
34+
`], { cwd: dir, encoding: 'utf8' });
35+
36+
assert.strictEqual(child.status, 0, child.stderr);
37+
const err = JSON.parse(child.stdout);
38+
assert.strictEqual(err.code, 'ENOENT');
39+
assert.strictEqual(err.path, undefined);
40+
assert.strictEqual(err.dest, missing);
41+
assert.strictEqual(err.message,
42+
`ENOENT: no such file or directory, chdir -> '${missing}'`);

0 commit comments

Comments
 (0)