Skip to content

Commit 078d009

Browse files
committed
SR-6968: Use more portable string decoding, dont assume UTF-8.
1 parent 83bd0d5 commit 078d009

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Foundation/ProcessInfo.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,17 @@ open class ProcessInfo: NSObject {
4242
}
4343

4444
open var environment: [String : String] {
45-
let equalSign = Int32(UInt8(ascii: "="))
45+
let equalSign = Character("=")
46+
let strEncoding = String.defaultCStringEncoding
47+
let envp = _CFEnviron()
4648
var env: [String : String] = [:]
4749
var idx = 0
48-
let envp = _CFEnviron()
4950

5051
while let entry = envp.advanced(by: idx).pointee {
51-
if let value = strchr(entry, equalSign) {
52-
let keyLen = entry.distance(to: value)
53-
guard keyLen > 0 else {
54-
continue
55-
}
56-
if let key = NSString(bytes: entry, length: keyLen, encoding: String.Encoding.utf8.rawValue) {
57-
env[key._swiftObject] = String(cString: value.advanced(by: 1))
58-
}
59-
} else {
60-
let key = String(cString: entry)
61-
env[key] = ""
52+
if let entry = String(cString: entry, encoding: strEncoding), let i = entry.index(of: equalSign) {
53+
let key = String(entry.prefix(upTo: i))
54+
let value = String(entry.suffix(from: i).dropFirst())
55+
env[key] = value
6256
}
6357
idx += 1
6458
}

0 commit comments

Comments
 (0)