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
for details.
@@ -4,17 +4,24 @@ package mount
/*
#include <stdio.h>
+#include <stdlib.h>
#include <sys/mnttab.h>
*/
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)
}
