Skip to content

Commit 7c0ed78

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #298 from BlueOwlOpenSource/fixes-sysctl-issue-on-mac
2 parents b887a0a + 18df4ec commit 7c0ed78

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Sources/Instrumentation/SDKResourceExtension/DataSource/DeviceDataSource.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@ public class DeviceDataSource: IDeviceDataSource {
2323
#else
2424
hwName[1] = HW_MACHINE
2525
#endif
26-
let machine = UnsafeMutablePointer<CChar>.allocate(capacity: 255)
26+
27+
// Returned 'error #12: Optional("Cannot allocate memory")' because len was not initialized properly.
28+
29+
let desiredLen = UnsafeMutablePointer<Int>.allocate(capacity: 1)
30+
let lenRequestError = sysctl(hwName, 2, nil, desiredLen, nil, 0)
31+
if lenRequestError != 0 {
32+
// TODO: better error log
33+
print("error #\(errno): \(String(describing: String(utf8String: strerror(errno))))")
34+
35+
return nil
36+
}
37+
38+
let machine = UnsafeMutablePointer<CChar>.allocate(capacity: desiredLen[0])
2739
let len: UnsafeMutablePointer<Int>! = UnsafeMutablePointer<Int>.allocate(capacity: 1)
40+
len[0] = desiredLen[0]
2841

29-
let error = sysctl(hwName, 2, machine, len, nil, 0)
30-
if error != 0 {
42+
let modelRequestError = sysctl(hwName, 2, machine, len, nil, 0)
43+
if modelRequestError != 0 {
3144
// TODO: better error log
3245
print("error #\(errno): \(String(describing: String(utf8String: strerror(errno))))")
3346

0 commit comments

Comments
 (0)