Avoid memory leak by using value rather than reference. (#3277)

Signed-off-by: Rolf Klemenz <tickez@madnet.ch>
This commit is contained in:
Rolf Klemenz 2025-03-26 15:30:46 +01:00 committed by GitHub
parent 5d458955be
commit 47e2bb34ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 13 deletions

View file

@ -89,7 +89,7 @@ type filesystemStats struct {
labels filesystemLabels labels filesystemLabels
size, free, avail float64 size, free, avail float64
files, filesFree float64 files, filesFree float64
purgeable *float64 purgeable float64
ro, deviceError float64 ro, deviceError float64
} }
@ -232,11 +232,10 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
c.mountInfoDesc, prometheus.GaugeValue, c.mountInfoDesc, prometheus.GaugeValue,
1.0, s.labels.device, s.labels.major, s.labels.minor, s.labels.mountPoint, 1.0, s.labels.device, s.labels.major, s.labels.minor, s.labels.mountPoint,
) )
if s.purgeable != nil { if s.purgeable >= 0 {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.purgeableDesc, prometheus.GaugeValue, c.purgeableDesc, prometheus.GaugeValue,
*s.purgeable, s.labels.device, s.labels.mountPoint, s.purgeable, s.labels.device, s.labels.mountPoint, s.labels.fsType, s.labels.deviceError,
s.labels.fsType, s.labels.deviceError,
) )
} }
} }

View file

@ -20,23 +20,22 @@ package collector
#cgo CFLAGS: -x objective-c #cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation #cgo LDFLAGS: -framework Foundation
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
Float64 *purgeable(char *path) { Float64 purgeable(char *path) {
CFNumberRef tmp; CFNumberRef tmp;
Float64 *value;
NSError *error = nil; NSError *error = nil;
NSString *str = [NSString stringWithUTF8String:path]; NSString *str = [NSString stringWithUTF8String:path];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:str]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:str];
NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error]; NSDictionary *results = [fileURL resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
if (results) { if (results) {
if ((tmp = CFDictionaryGetValue((CFDictionaryRef)results, NSURLVolumeAvailableCapacityForImportantUsageKey)) == NULL) if ((tmp = CFDictionaryGetValue((CFDictionaryRef)results, NSURLVolumeAvailableCapacityForImportantUsageKey)) == NULL) {
return NULL; return -1.0f;
value = (Float64 *)malloc(sizeof(Float64)); }
if (CFNumberGetValue(tmp, kCFNumberFloat64Type, value)) { Float64 value;
if (CFNumberGetValue(tmp, kCFNumberFloat64Type, &value)) {
return value; return value;
} }
} }
free(value); return -1.0f;
return NULL;
} }
*/ */
import "C" import "C"
@ -100,7 +99,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
avail: float64(mnt[i].f_bavail) * float64(mnt[i].f_bsize), avail: float64(mnt[i].f_bavail) * float64(mnt[i].f_bsize),
files: float64(mnt[i].f_files), files: float64(mnt[i].f_files),
filesFree: float64(mnt[i].f_ffree), filesFree: float64(mnt[i].f_ffree),
purgeable: (*float64)(C.purgeable(C.CString(mountpoint))), purgeable: float64(C.purgeable(C.CString(mountpoint))),
ro: ro, ro: ro,
}) })
} }