Skip to content

Commit 66b4352

Browse files
tickezSuperQ
authored andcommitted
Avoid memory leak by using value rather than reference. (#3277)
Signed-off-by: Rolf Klemenz <[email protected]>
1 parent 02afa5c commit 66b4352

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

collector/filesystem_common.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type filesystemStats struct {
8989
labels filesystemLabels
9090
size, free, avail float64
9191
files, filesFree float64
92-
purgeable *float64
92+
purgeable float64
9393
ro, deviceError float64
9494
}
9595

@@ -232,11 +232,10 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
232232
c.mountInfoDesc, prometheus.GaugeValue,
233233
1.0, s.labels.device, s.labels.major, s.labels.minor, s.labels.mountPoint,
234234
)
235-
if s.purgeable != nil {
235+
if s.purgeable >= 0 {
236236
ch <- prometheus.MustNewConstMetric(
237237
c.purgeableDesc, prometheus.GaugeValue,
238-
*s.purgeable, s.labels.device, s.labels.mountPoint,
239-
s.labels.fsType, s.labels.deviceError,
238+
s.purgeable, s.labels.device, s.labels.mountPoint, s.labels.fsType, s.labels.deviceError,
240239
)
241240
}
242241
}

collector/filesystem_macos.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ package collector
2020
#cgo CFLAGS: -x objective-c
2121
#cgo LDFLAGS: -framework Foundation
2222
#import <Foundation/Foundation.h>
23-
Float64 *purgeable(char *path) {
23+
Float64 purgeable(char *path) {
2424
CFNumberRef tmp;
25-
Float64 *value;
2625
NSError *error = nil;
2726
NSString *str = [NSString stringWithUTF8String:path];
2827
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:str];
2928
NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
3029
if (results) {
31-
if ((tmp = CFDictionaryGetValue((CFDictionaryRef)results, NSURLVolumeAvailableCapacityForImportantUsageKey)) == NULL)
32-
return NULL;
33-
value = (Float64 *)malloc(sizeof(Float64));
34-
if (CFNumberGetValue(tmp, kCFNumberFloat64Type, value)) {
30+
if ((tmp = CFDictionaryGetValue((CFDictionaryRef)results, NSURLVolumeAvailableCapacityForImportantUsageKey)) == NULL) {
31+
return -1.0f;
32+
}
33+
Float64 value;
34+
if (CFNumberGetValue(tmp, kCFNumberFloat64Type, &value)) {
3535
return value;
3636
}
3737
}
38-
free(value);
39-
return NULL;
38+
return -1.0f;
4039
}
4140
*/
4241
import "C"
@@ -100,7 +99,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
10099
avail: float64(mnt[i].f_bavail) * float64(mnt[i].f_bsize),
101100
files: float64(mnt[i].f_files),
102101
filesFree: float64(mnt[i].f_ffree),
103-
purgeable: (*float64)(C.purgeable(C.CString(mountpoint))),
102+
purgeable: float64(C.purgeable(C.CString(mountpoint))),
104103
ro: ro,
105104
})
106105
}

0 commit comments

Comments
 (0)