Skip to content

fix: build PATH env var using OsString instead of String#15360

Open
bolinfest wants to merge 1 commit intomainfrom
pr15360
Open

fix: build PATH env var using OsString instead of String#15360
bolinfest wants to merge 1 commit intomainfrom
pr15360

Conversation

@bolinfest
Copy link
Collaborator

@bolinfest bolinfest commented Mar 21, 2026

Why

prepend_path_entry_for_codex_aliases() was rebuilding PATH through std::env::var("PATH") and String formatting. That assumes the inherited PATH is valid UTF-8, which is not guaranteed on supported platforms. When PATH contains non-UTF-8 bytes, Codex can fail to prepend the per-session helper directory even though the existing environment is otherwise usable.

What changed

  • read PATH with std::env::var_os() instead of std::env::var()
  • construct the updated value as an OsString instead of a formatted String
  • preserve the existing PATH contents while still prepending the arg0 helper directory

How to verify

  • Not run locally; change is limited to how PATH is reconstructed before std::env::set_var().

Stack created with Sapling. Best reviewed with ReviewStack.

@bolinfest bolinfest changed the title fix: build env var using OsString instead of String fix: preserve non-UTF-8 PATH entries in arg0 setup Mar 21, 2026
@bolinfest bolinfest changed the title fix: preserve non-UTF-8 PATH entries in arg0 setup fix: PATH build env var using OsString instead of String Mar 21, 2026
@bolinfest bolinfest changed the title fix: PATH build env var using OsString instead of String fix: build PATH env var using OsString instead of String Mar 21, 2026
Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a73d681558

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +312 to +321
let updated_path_env_var = match std::env::var_os("PATH") {
Some(existing_path) => {
let mut path_env_var =
std::ffi::OsString::with_capacity(path.as_os_str().len() + 1 + existing_path.len());
path_env_var.push(path);
path_env_var.push(PATH_SEPARATOR);
path_env_var.push(existing_path);
path_env_var
}
None => path.as_os_str().to_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not leave a non-Unicode PATH in the process env

On Unix, var_os("PATH") can return raw bytes, so this now preserves a non-Unicode PATH in the current process. Startup still succeeds, but later code paths still assume the environment is UTF-8: core/src/exec_env.rs:24 (used by the shell tool in core/src/tools/handlers/shell.rs:75 and unified exec in core/src/unified_exec/process_manager.rs:586) and shell-escalation/src/unix/escalate_client.rs:48 both iterate with std::env::vars(), which panics when PATH is not Unicode. Before this change the Err(NotUnicode) branch replaced PATH with the helper dir's UTF-8 string, so those flows kept working; with this version, users who launch Codex from a shell with a non-UTF8 path entry can crash as soon as they run a shell or escalated command.

Useful? React with 👍 / 👎.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll address this in a follow-up.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant