Skip to content

src: do not report an unread cwd in process.chdir() errors - #67840

Draft
Marti2203 wants to merge 1 commit into
nodejs:mainfrom
Marti2203:fix/process-chdir-deleted-cwd
Draft

Marti2203 wants to merge 1 commit into
nodejs:mainfrom
Marti2203:fix/process-chdir-deleted-cwd

Conversation

@Marti2203

Copy link
Copy Markdown

When process.chdir() fails, the thrown error includes the original working
directory, read with uv_cwd(). The result of uv_cwd() was not checked: if
the working directory has been removed, uv_cwd() fails, the buffer is never
written, and the error reports uninitialized stack bytes as its path (and in
its message):

$ mkdir /tmp/d && cd /tmp/d
$ node -e "require('fs').rmdirSync(process.cwd()); try { process.chdir('/nonexistent') } catch (e) { console.log(JSON.stringify(e.path)) }"
"\u0012"

With this change the original directory is left out of the error when it
cannot be determined (chdir -> '/nonexistent', no path). The new test runs
a child process in a removed directory and checks the error's path, dest
and message; it is skipped on Windows, where the working directory cannot be
removed.

This bug was found by an LLM-assisted static analysis tool; the fix and test were drafted with
the help of a coding agent. I reproduced the stray path on v22.22.2 and verified the fix on
Linux (the new test fails before the change and passes after it), and ran the existing
test-process-chdir* tests, cpplint and ESLint.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@MikeMcC399

Copy link
Copy Markdown
Member

You have multiple unapproved PRs open at this time. To stay within the guidelines, you may de-prioritize this PR or other open PRs by setting them to draft status.

@Marti2203
Marti2203 marked this pull request as draft November 28, 2026 07:18
@Marti2203
Marti2203 force-pushed the fix/process-chdir-deleted-cwd branch from e48e504 to 0d0512f Compare November 28, 2026 07:19
@Marti2203

Copy link
Copy Markdown
Author

Sorry for the delay. Done: this is now a draft, and #67060 is the only PR I have under review. The commit is also signed off now.

@MikeMcC399

This comment was marked as resolved.

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>
@Marti2203
Marti2203 force-pushed the fix/process-chdir-deleted-cwd branch from 0d0512f to a957d5d Compare November 28, 2026 07:36
@Marti2203

Copy link
Copy Markdown
Author

Thanks, and sorry about that. I've rewrapped the commit message to 72 columns, and the lint check passes now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:automated-account c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants