Skip to content

Commit dc49e15

Browse files
authored
Hide Windows magic environment values from ProcessInfo.environment (#853)
Windows GetEnvironmentStringsW API can return magic environment variables set by the cmd shell that starts with "=". We should hide these values to avoid surprising behavior. resolves: #847
1 parent 234f225 commit dc49e15

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ final class _ProcessInfo: Sendable {
7575
for env in environments {
7676
let environmentString = String(cString: env)
7777

78+
#if os(Windows)
79+
// Windows GetEnvironmentStringsW API can return
80+
// magic environment variables set by the cmd shell
81+
// that starts with `=`
82+
// We should exclude these values
83+
if environmentString.utf8.first == ._equal {
84+
continue
85+
}
86+
#endif // os(Windows)
87+
7888
guard let delimiter = environmentString.firstIndex(of: "=") else {
7989
continue
8090
}

Tests/FoundationEssentialsTests/ProcessInfoTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,14 @@ final class ProcessInfoTests : XCTestCase {
189189
processInfo.processName = originalProcessName
190190
XCTAssertEqual(processInfo.processName, originalProcessName)
191191
}
192+
193+
func testWindowsEnvironmentDoesNotContainMagicValues() {
194+
// Windows GetEnvironmentStringsW API can return
195+
// magic environment variables set by the cmd shell
196+
// that starts with `=`
197+
// This test makes sure we don't include these
198+
// magic variables
199+
let env = ProcessInfo.processInfo.environment
200+
XCTAssertNil(env[""])
201+
}
192202
}

0 commit comments

Comments
 (0)