fix: harden device model decoding

This commit is contained in:
Peter Steinberger
2025-12-20 01:48:10 +01:00
parent afa4a234f9
commit 1b38ee8b46
3 changed files with 9 additions and 6 deletions

View File

@@ -40,7 +40,8 @@ enum InstanceIdentity {
guard sysctlbyname("hw.model", &buffer, &size, nil, 0) == 0 else { return nil }
let bytes = buffer.prefix { $0 != 0 }.map { UInt8(bitPattern: $0) }
let s = String(decoding: bytes, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)
return s.isEmpty ? nil : s
guard let raw = String(bytes: bytes, encoding: .utf8) else { return nil }
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}()
}