Skip to content

Hide Windows magic environment values from ProcessInfo.environment #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ final class _ProcessInfo: Sendable {
for env in environments {
let environmentString = String(cString: env)

#if os(Windows)
// Windows GetEnvironmentStringsW API can return
// magic environment variables set by the cmd shell
// that starts with `=`
// We should exclude these values
if environmentString.utf8.first == ._equal {
continue
}
#endif // os(Windows)

guard let delimiter = environmentString.firstIndex(of: "=") else {
continue
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/FoundationEssentialsTests/ProcessInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,14 @@ final class ProcessInfoTests : XCTestCase {
processInfo.processName = originalProcessName
XCTAssertEqual(processInfo.processName, originalProcessName)
}

func testWindowsEnvironmentDoesNotContainMagicValues() {
// Windows GetEnvironmentStringsW API can return
// magic environment variables set by the cmd shell
// that starts with `=`
// This test makes sure we don't include these
// magic variables
let env = ProcessInfo.processInfo.environment
XCTAssertNil(env[""])
}
}