Skip to content

Fix Coverity NullReferenceException Issues w/ Remotes #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden

using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
{
var gitCallbacks = new GitRemoteCallbacks {version = 1};
var gitCallbacks = new GitRemoteCallbacks { version = 1 };

if (credentialsProvider != null)
{
Expand Down Expand Up @@ -111,13 +111,13 @@ static RemoteSafeHandle BuildRemoteSafeHandle(RepositorySafeHandle repoHandle, R
{
Debug.Assert((remote == null) ^ (url == null));

RemoteSafeHandle remoteHandle;
RemoteSafeHandle remoteHandle = null;

if (url != null)
{
remoteHandle = Proxy.git_remote_create_anonymous(repoHandle, url);
}
else
else if (remote != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're actually asserting to protect against this. And the call to Debug.Assert is "seen" by Coverity, however it looks like the tool doesn't properly take it into account in the code flow analysis.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - the ^ operator is the exclusive-or operator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverity is correct. We in fact do assert but only in debug mode. We then have code flow for when both are null which should be an impossibility. We even dereference a null value in the malformed code flow that we assert, but do not check, will not happen.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is correct. However, this is not a public method so we do control all callers of this method. Either way, I agree that this is a bit fragile. What would you think about throwing an exception in the case where we get unexpected input? Or, performing the refactoring suggested in #1084. As you mentioned, I think everyone agrees on the ultimate structure for this code (two methods).

{
remoteHandle = Proxy.git_remote_lookup(repoHandle, remote.Name, true);
}
Expand Down Expand Up @@ -148,14 +148,14 @@ static void DoFetch(RepositorySafeHandle repoHandle, Remote remote, string url,
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
var fetchOptions = new GitFetchOptions
{
RemoteCallbacks = gitCallbacks,
download_tags = Proxy.git_remote_autotag(remoteHandle),
};
{
RemoteCallbacks = gitCallbacks,
download_tags = Proxy.git_remote_autotag(remoteHandle),
};

if (options.TagFetchMode.HasValue)
{
fetchOptions.download_tags = options.TagFetchMode.Value;
fetchOptions.download_tags = options.TagFetchMode.Value;
}

Proxy.git_remote_fetch(remoteHandle, refspecs, fetchOptions, logMessage);
Expand Down