From 20ca0f1376199648b1dece3a4b6b0f99e9ec9a9d Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Sat, 31 Dec 2016 03:08:17 +0100 Subject: [PATCH] Eliminate memory leak in FreeBSD devstat collector The memory allocated by calloc was never freed. Since the devinfo struct never leaves the function, anyway, we might as well just allocate it on the stack. --- collector/devstat_freebsd.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/collector/devstat_freebsd.go b/collector/devstat_freebsd.go index e4be57ff..7ea5a22a 100644 --- a/collector/devstat_freebsd.go +++ b/collector/devstat_freebsd.go @@ -64,11 +64,8 @@ typedef struct { int _get_ndevs() { struct statinfo current; - int num_devices; - - current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); - if (current.dinfo == NULL) - return -2; + struct devinfo info = {}; + current.dinfo = &info; devstat_checkversion(NULL); @@ -80,12 +77,11 @@ int _get_ndevs() { Stats _get_stats(int i) { struct statinfo current; - int num_devices; + struct devinfo info = {}; + current.dinfo = &info; - current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); devstat_getdevs(NULL, ¤t); - num_devices = current.dinfo->numdevs; Stats stats; uint64_t bytes_read, bytes_write, bytes_free; uint64_t transfers_other, transfers_read, transfers_write, transfers_free; @@ -186,9 +182,6 @@ func (c *devstatCollector) Update(ch chan<- prometheus.Metric) (err error) { if count == -1 { return errors.New("devstat_getdevs() failed") } - if count == -2 { - return errors.New("calloc() failed") - } for i := C.int(0); i < count; i++ { stats := C._get_stats(i)