From aac89c524e2966385de43ae50f8fdf93243264ca Mon Sep 17 00:00:00 2001 From: Kai Storbeck Date: Tue, 19 Apr 2016 21:59:53 +0200 Subject: [PATCH 1/3] This should fix it. --- collector/cpu_freebsd.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index 29748f11..7b4e7bc7 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -103,6 +103,8 @@ void freeCPUTimes(double *cpu_times) { */ import "C" +const MAXCPUTIMESLEN = C.MAXCPU * C.CPUSTATES; + type statCollector struct { cpu *prometheus.CounterVec } @@ -149,10 +151,13 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { if C.getCPUTimes(&ncpu, &cpuTimesC, &cpuTimesLength) == -1 { return errors.New("could not retrieve CPU times") } - defer C.freeCPUTimes(cpuTimesC) + defer C.free(unsafe.Pointer(cpuTimesC)) + if cpuTimesLength > MAXCPUTIMESLEN { + return errors.New("more CPU's than MAXCPU?") + } // Convert C.double array to Go array (https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices). - cpuTimes := (*[1 << 30]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] + cpuTimes := (*[maxCpuTimes]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] for cpu := 0; cpu < int(ncpu); cpu++ { base_idx := C.CPUSTATES * cpu From 365e688cecfefdae625f5c5a73dcc07846a9d0b2 Mon Sep 17 00:00:00 2001 From: Kai Storbeck Date: Wed, 20 Apr 2016 00:19:16 +0200 Subject: [PATCH 2/3] gofmt, a typo, and an unintended change crept in --- collector/cpu_freebsd.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index 7b4e7bc7..59c8157b 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -103,7 +103,7 @@ void freeCPUTimes(double *cpu_times) { */ import "C" -const MAXCPUTIMESLEN = C.MAXCPU * C.CPUSTATES; +const MAXCPUTIMESLEN = C.MAXCPU * C.CPUSTATES type statCollector struct { cpu *prometheus.CounterVec @@ -151,13 +151,13 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { if C.getCPUTimes(&ncpu, &cpuTimesC, &cpuTimesLength) == -1 { return errors.New("could not retrieve CPU times") } - defer C.free(unsafe.Pointer(cpuTimesC)) + defer C.freeCPUTimes(cpuTimesC) if cpuTimesLength > MAXCPUTIMESLEN { return errors.New("more CPU's than MAXCPU?") } // Convert C.double array to Go array (https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices). - cpuTimes := (*[maxCpuTimes]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] + cpuTimes := (*[MAXCPUTIMESLEN]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] for cpu := 0; cpu < int(ncpu); cpu++ { base_idx := C.CPUSTATES * cpu From dcfbf40dbffff63b3f286c85e6f47ae7297f5608 Mon Sep 17 00:00:00 2001 From: Kai Storbeck Date: Wed, 20 Apr 2016 00:22:47 +0200 Subject: [PATCH 3/3] change caps of the constant --- collector/cpu_freebsd.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index 59c8157b..0ede1a69 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -103,7 +103,7 @@ void freeCPUTimes(double *cpu_times) { */ import "C" -const MAXCPUTIMESLEN = C.MAXCPU * C.CPUSTATES +const maxCPUTimesLen = C.MAXCPU * C.CPUSTATES type statCollector struct { cpu *prometheus.CounterVec @@ -152,12 +152,12 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) (err error) { return errors.New("could not retrieve CPU times") } defer C.freeCPUTimes(cpuTimesC) - if cpuTimesLength > MAXCPUTIMESLEN { + if cpuTimesLength > maxCPUTimesLen { return errors.New("more CPU's than MAXCPU?") } // Convert C.double array to Go array (https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices). - cpuTimes := (*[MAXCPUTIMESLEN]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] + cpuTimes := (*[maxCPUTimesLen]C.double)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength] for cpu := 0; cpu < int(ncpu); cpu++ { base_idx := C.CPUSTATES * cpu