Skip to content

Commit

Permalink
Merge pull request #1767 from chuangbo/fix/memory-leak-and-string-cor…
Browse files Browse the repository at this point in the history
…ruption-in-arm-mac-temperature-sensors

fix: memory leak and string corruption in ARM Mac temperature sensors
  • Loading branch information
shirou authored Dec 30, 2024
2 parents b8e1b28 + 19082b3 commit bcd0c0a
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions sensors/sensors_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
}

ta.matching(0xff00, 5)
thermalNames := ta.getProductNames()
thermalValues := ta.getThermalValues()
defer ta.cfRelease(uintptr(ta.sensors))

// Create HID system client
system := ta.ioHIDEventSystemClientCreate(common.KCFAllocatorDefault)
defer ta.cfRelease(uintptr(system))

thermalNames := ta.getProductNames(system)
thermalValues := ta.getThermalValues(system)
result := dumpNameValues(thermalNames, thermalValues)

ta.cfRelease(uintptr(ta.sensors))
return result, nil
}

Expand Down Expand Up @@ -84,59 +89,57 @@ type temperatureArm struct {
sensors unsafe.Pointer
}

func (ta *temperatureArm) getProductNames() []string {
func (ta *temperatureArm) getProductNames(system unsafe.Pointer) []string {
ioHIDServiceClientCopyProperty := common.GetFunc[common.IOHIDServiceClientCopyPropertyFunc](ta.ioKit, common.IOHIDServiceClientCopyPropertySym)

cfStringGetLength := common.GetFunc[common.CFStringGetLengthFunc](ta.cf, common.CFStringGetLengthSym)
cfStringGetCString := common.GetFunc[common.CFStringGetCStringFunc](ta.cf, common.CFStringGetCStringSym)

var names []string
system := ta.ioHIDEventSystemClientCreate(common.KCFAllocatorDefault)

ta.ioHIDEventSystemClientSetMatching(uintptr(system), uintptr(ta.sensors))
matchingsrvs := ta.ioHIDEventSystemClientCopyServices(uintptr(system))

if matchingsrvs == nil {
return nil
}
defer ta.cfRelease(uintptr(matchingsrvs))

count := ta.cfArrayGetCount(uintptr(matchingsrvs))

var i int32
str := ta.cfStr("Product")
defer ta.cfRelease(uintptr(str))

for i = 0; i < count; i++ {
sc := ta.cfArrayGetValueAtIndex(uintptr(matchingsrvs), i)
name := ioHIDServiceClientCopyProperty(uintptr(sc), uintptr(str))

if name != nil {
length := cfStringGetLength(uintptr(name)) + 1 // null terminator
buf := make([]byte, length-1)
length := cfStringGetLength(uintptr(name)) + 1 // include null terminator
buf := make([]byte, length) // allocate buffer with full length
cfStringGetCString(uintptr(name), &buf[0], length, common.KCFStringEncodingUTF8)

names = append(names, string(buf))
names = append(names, string(buf[:length-1])) // remove null terminator
ta.cfRelease(uintptr(name))
} else {
names = append(names, "noname")
}
}

ta.cfRelease(uintptr(matchingsrvs))
ta.cfRelease(uintptr(str))
return names
}

func (ta *temperatureArm) getThermalValues() []float64 {
func (ta *temperatureArm) getThermalValues(system unsafe.Pointer) []float64 {
ioHIDServiceClientCopyEvent := common.GetFunc[common.IOHIDServiceClientCopyEventFunc](ta.ioKit, common.IOHIDServiceClientCopyEventSym)
ioHIDEventGetFloatValue := common.GetFunc[common.IOHIDEventGetFloatValueFunc](ta.ioKit, common.IOHIDEventGetFloatValueSym)

system := ta.ioHIDEventSystemClientCreate(common.KCFAllocatorDefault)

ta.ioHIDEventSystemClientSetMatching(uintptr(system), uintptr(ta.sensors))
matchingsrvs := ta.ioHIDEventSystemClientCopyServices(uintptr(system))

if matchingsrvs == nil {
return nil
}
defer ta.cfRelease(uintptr(matchingsrvs))

count := ta.cfArrayGetCount(uintptr(matchingsrvs))

Expand All @@ -155,7 +158,6 @@ func (ta *temperatureArm) getThermalValues() []float64 {
values = append(values, temp)
}

ta.cfRelease(uintptr(matchingsrvs))
return values
}

Expand Down

0 comments on commit bcd0c0a

Please sign in to comment.