Skip to content

Commit 0a485f5

Browse files
authored
Merge pull request #2991 from spevans/pr_cpu_count_bad_input
2 parents 8893f51 + 8df6184 commit 0a485f5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Sources/Foundation/ProcessInfo.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,26 @@ open class ProcessInfo: NSObject {
319319
}
320320

321321
// These are internal access for testing
322-
static func countCoreIds(cores: Substring) -> Int {
322+
static func countCoreIds(cores: Substring) -> Int? {
323323
let ids = cores.split(separator: "-", maxSplits: 1)
324324
guard let first = ids.first.flatMap({ Int($0, radix: 10) }),
325325
let last = ids.last.flatMap({ Int($0, radix: 10) }),
326326
last >= first
327-
else { preconditionFailure("cpuset format is incorrect") }
327+
else {
328+
return nil
329+
}
328330
return 1 + last - first
329331
}
330332

331333
static func coreCount(cpuset cpusetPath: String) -> Int? {
332334
guard let cpuset = try? firstLineOfFile(path: cpusetPath).split(separator: ","),
333335
!cpuset.isEmpty
334336
else { return nil }
335-
336-
return cpuset.map(countCoreIds).reduce(0, +)
337+
if let first = cpuset.first, let count = countCoreIds(cores: first) {
338+
return count
339+
} else {
340+
return nil
341+
}
337342
}
338343

339344
static func coreCount(quota quotaPath: String, period periodPath: String) -> Int? {

0 commit comments

Comments
 (0)