Remote: don't mix allocators

We cannot ask libgit2 to free the memory we have allocated ourselves, as
it cannot know how to do it. Let's free the strarray ourselves.
This commit is contained in:
Carlos Martín Nieto 2014-03-19 07:54:52 +01:00
parent 3274d477c9
commit ad128bdefb
2 changed files with 42 additions and 2 deletions

View File

@ -303,6 +303,19 @@ func makeCStringsFromStrings(s []string) **C.char {
return x
}
func freeStrarray(arr *C.git_strarray) {
count := int(arr.count)
size := unsafe.Sizeof(unsafe.Pointer(nil))
i := 0
for p := uintptr(unsafe.Pointer(arr.strings)); i < count; p += size {
C.free(unsafe.Pointer(sptr(p)))
i++
}
C.free(unsafe.Pointer(arr.strings))
}
func (o *Remote) GetFetchRefspecs() ([]string, error) {
crefspecs := C.git_strarray{}
@ -323,7 +336,7 @@ func (o *Remote) SetFetchRefspecs(refspecs []string) error {
crefspecs := C.git_strarray{}
crefspecs.count = C.size_t(len(refspecs))
crefspecs.strings = makeCStringsFromStrings(refspecs)
defer C.git_strarray_free(&crefspecs)
defer freeStrarray(&crefspecs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -368,7 +381,7 @@ func (o *Remote) SetPushRefspecs(refspecs []string) error {
crefspecs := C.git_strarray{}
crefspecs.count = C.size_t(len(refspecs))
crefspecs.strings = makeCStringsFromStrings(refspecs)
defer C.git_strarray_free(&crefspecs)
defer freeStrarray(&crefspecs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

27
remote_test.go Normal file
View File

@ -0,0 +1,27 @@
package git
import (
"os"
"testing"
)
func TestRefspecs(t *testing.T) {
repo := createTestRepo(t)
defer os.RemoveAll(repo.Workdir())
remote, err := repo.CreateRemoteInMemory("refs/heads/*:refs/heads/*", "git://foo/bar")
checkFatal(t, err)
expected := []string{
"refs/heads/*:refs/remotes/origin/*",
"refs/pull/*/head:refs/remotes/origin/*",
}
err = remote.SetFetchRefspecs(expected)
checkFatal(t, err)
actual, err := remote.GetFetchRefspecs()
checkFatal(t, err)
compareStringList(t, expected, actual)
}