Skip to content

Commit 2992d02

Browse files
authored
Fix CreateProcessW failure caused by REG_MULTI_SZ environment variables (#11714)
1 parent 5becb10 commit 2992d02

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

app/src/terminal/local_tty/windows/environment.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ fn add_user_env(env: &mut BTreeMap<OsString, EnvEntry>) {
297297
}
298298

299299
fn reg_value_to_string(value: &RegValue, key: &str) -> anyhow::Result<OsString> {
300+
// Only REG_SZ and REG_EXPAND_SZ are valid for environment variables.
301+
// https://github.com/microsoft/terminal/blob/1ba28b298f677d838c3a2e457a8a1f569bff6299/src/inc/til/env.h#L247-L259
302+
if value.vtype != RegType::REG_SZ && value.vtype != RegType::REG_EXPAND_SZ {
303+
anyhow::bail!(
304+
"Unsupported registry type {:?} for env var {key:?}",
305+
value.vtype
306+
);
307+
}
308+
300309
let key_lower = key.to_ascii_lowercase();
301310
// RegType::REG_EXPAND_SZ requires expansion of nested env vars, e.g. %USERPROFILE%\AppData to
302311
// C:\Users\andy\AppData
@@ -329,7 +338,13 @@ fn reg_value_to_string(value: &RegValue, key: &str) -> anyhow::Result<OsString>
329338
};
330339

331340
// These are null-terminated, but we don't want the terminator here. We add it back later.
332-
os_str.map(|v| v.to_string_lossy().trim_end_matches('\0').into())
341+
os_str.map(|v| {
342+
let s = v.to_string_lossy();
343+
match s.find('\0') {
344+
Some(pos) => s[..pos].into(),
345+
None => v,
346+
}
347+
})
333348
}
334349

335350
/// Best-effort lowercase transformation of an OsString.

0 commit comments

Comments
 (0)