libgit2: dispose git2go objects when error != nil

getBlankRepoAndRemote's callers are responsible for the disposal
of the returned objects. However, the caller does not expect to
need to dispose objects when err != nil, which may result to memory
leaks.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
This commit is contained in:
Paulo Gomes 2022-05-09 17:27:44 +01:00
parent 54e07d8783
commit 44e166e576
No known key found for this signature in database
GPG Key ID: 9995233870E99BEE
1 changed files with 3 additions and 0 deletions

View File

@ -428,12 +428,15 @@ func getBlankRepoAndRemote(ctx context.Context, path, url string, opts *git.Auth
remote, err := repo.Remotes.Create("origin", url)
if err != nil {
repo.Free()
return nil, nil, fmt.Errorf("unable to create remote for '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
}
callBacks := RemoteCallbacks(ctx, opts)
err = remote.ConnectFetch(&callBacks, &git2go.ProxyOptions{Type: git2go.ProxyTypeAuto}, nil)
if err != nil {
remote.Free()
repo.Free()
return nil, nil, fmt.Errorf("unable to fetch-connect to remote '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
}
return repo, remote, nil