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.
This commit is contained in:
Dominik Honnef 2016-12-31 03:08:17 +01:00
parent 3b469e5547
commit 20ca0f1376

View file

@ -64,11 +64,8 @@ typedef struct {
int _get_ndevs() { int _get_ndevs() {
struct statinfo current; struct statinfo current;
int num_devices; struct devinfo info = {};
current.dinfo = &info;
current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
if (current.dinfo == NULL)
return -2;
devstat_checkversion(NULL); devstat_checkversion(NULL);
@ -80,12 +77,11 @@ int _get_ndevs() {
Stats _get_stats(int i) { Stats _get_stats(int i) {
struct statinfo current; struct statinfo current;
int num_devices; struct devinfo info = {};
current.dinfo = &info;
current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
devstat_getdevs(NULL, &current); devstat_getdevs(NULL, &current);
num_devices = current.dinfo->numdevs;
Stats stats; Stats stats;
uint64_t bytes_read, bytes_write, bytes_free; uint64_t bytes_read, bytes_write, bytes_free;
uint64_t transfers_other, transfers_read, transfers_write, transfers_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 { if count == -1 {
return errors.New("devstat_getdevs() failed") return errors.New("devstat_getdevs() failed")
} }
if count == -2 {
return errors.New("calloc() failed")
}
for i := C.int(0); i < count; i++ { for i := C.int(0); i < count; i++ {
stats := C._get_stats(i) stats := C._get_stats(i)