Skip to content

Commit

Permalink
#5847: Fix crash due to double-freeing the credentials.
Browse files Browse the repository at this point in the history
libgit2 will keep asking the user code for credentials, and it will be freeing it after every use, so we can't deliver the same instance again.
  • Loading branch information
codereader committed Feb 28, 2022
1 parent 40f6a80 commit ab8d7fe
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions plugins/vcs/Remote.h
Expand Up @@ -41,13 +41,9 @@ class Remote final

git_fetch_options options = GIT_FETCH_OPTIONS_INIT;

auto credentials = getCredentialsForRemote(url);

if (credentials != nullptr)
{
options.callbacks.credentials = AcquireCredentials;
options.callbacks.payload = credentials;
}
// We will be asked for credentials when the server asks libgit
options.callbacks.credentials = AcquireCredentials;
options.callbacks.payload = this;

auto remoteName = git_remote_name(_remote);

Expand All @@ -71,13 +67,10 @@ class Remote final
};

auto url = wxURI(git_remote_url(_remote));
auto credentials = getCredentialsForRemote(url);

if (credentials != nullptr)
{
options.callbacks.credentials = AcquireCredentials;
options.callbacks.payload = credentials;
}
// We will be asked for credentials when the server asks libgit
options.callbacks.credentials = AcquireCredentials;
options.callbacks.payload = this;

auto remoteName = git_remote_name(_remote);
rMessage() << "Pushing to remote " << remoteName << std::endl;
Expand Down Expand Up @@ -109,14 +102,16 @@ class Remote final

static int AcquireCredentials(git_cred** out, const char* url, const char* username_from_url, unsigned int allowed_types, void* payload)
{
*out = reinterpret_cast<git_credential*>(payload);
return 0;
*out = GetCredentialsForRemote(url);
return *out == nullptr ? GIT_PASSTHROUGH : 0;
}

git_credential* getCredentialsForRemote(const wxURI& url)
static git_credential* GetCredentialsForRemote(const std::string& remoteUrl)
{
wxURI uri(remoteUrl);

// Create the git:scheme://server string to query the credential manager
auto credentialResource = fmt::format("git:{0}://{1}", url.GetScheme().ToStdString(), url.GetServer().ToStdString());
auto credentialResource = fmt::format("git:{0}://{1}", uri.GetScheme().ToStdString(), uri.GetServer().ToStdString());

auto userAndPass = CredentialManager::RetrievePassword(credentialResource);

Expand Down

0 comments on commit ab8d7fe

Please sign in to comment.