From 4a582405b90dfcbe802dbce715224d82063a0347 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 15 Sep 2017 10:55:53 +0200 Subject: [PATCH] mount: fix CString memory leaks Make sure to call C.free on C strings allocated using C.CString. C.CString allocates memory in the C heap using malloc. It is the callers responsibility to free them. See https://golang.org/cmd/cgo/#hdr-Go_references_to_C for details. Signed-off-by: Tobias Klauser --- mount/mountinfo_solaris.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mount/mountinfo_solaris.go b/mount/mountinfo_solaris.go index 9ba5c3d44..aaafad36a 100644 --- a/mount/mountinfo_solaris.go +++ b/mount/mountinfo_solaris.go @@ -4,17 +4,24 @@ package mount /* #include +#include #include */ import "C" import ( "fmt" + "unsafe" ) // Self retrieves a list of mounts for the current running process. func Self() ([]Info, error) { - mnttab := C.fopen(C.CString(C.MNTTAB), C.CString("r")) + path := C.CString(C.MNTTAB) + defer C.free(unsafe.Pointer(path)) + mode := C.CString("r") + defer C.free(unsafe.Pointer(mode)) + + mnttab := C.fopen(path, mode) if mnttab == nil { return nil, fmt.Errorf("Failed to open %s", C.MNTTAB) }