conformal / gotk3

Go bindings for GTK3
ISC License
470 stars 81 forks source link

Potential unsafe memory access #86

Open maxnordlund opened 10 years ago

maxnordlund commented 10 years ago

I was browsing through the code today and noticed something that looked odd:

    cstr := C.CString(v)
    defer C.free(unsafe.Pointer(cstr))
    p = unsafe.Pointer(cstr)

    ...

    return p

Won't this create a unsafe memory access?

jrick commented 10 years ago

Yes, it's a use after free. Nice catch.

Since pointerVal is only called by Object.Set, and I think the memory handling for other types is incorrect as well, I think that whole function body should be moved into Object.Set.

The other memory problem here is that while returning an unsafe.Pointer does cause the pointed to Go value to escape to heap, the value is never freed if it is passed to a C function as a void * and is not type converted back to a Go pointer. So while the other types don't cause undefined behavior like strings do here, they are still memory leaks.