Merge pull request #4253 from pks-t/pks/cov-fixes

Coverity fixes
diff --git a/src/fileops.c b/src/fileops.c
index a0a2795..bda17f0 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -1155,9 +1155,13 @@
 
 int git_futils_fsync_parent(const char *path)
 {
-	char *parent = git_path_dirname(path);
-	int error = git_futils_fsync_dir(parent);
+	char *parent;
+	int error;
 
+	if ((parent = git_path_dirname(path)) == NULL)
+		return -1;
+
+	error = git_futils_fsync_dir(parent);
 	git__free(parent);
 	return error;
 }
diff --git a/src/path.c b/src/path.c
index b7205a6..5fc7a05 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1708,6 +1708,7 @@
 	unsigned int flags)
 {
 	int protectHFS = 0, protectNTFS = 0;
+	int error = 0;
 
 	flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL;
 
@@ -1720,13 +1721,13 @@
 #endif
 
 	if (repo && !protectHFS)
-		git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
-	if (protectHFS)
+		error = git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
+	if (!error && protectHFS)
 		flags |= GIT_PATH_REJECT_DOT_GIT_HFS;
 
 	if (repo && !protectNTFS)
-		git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
-	if (protectNTFS)
+		error = git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
+	if (!error && protectNTFS)
 		flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;
 
 	return flags;
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index b325d27..988a14b 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1140,7 +1140,7 @@
 static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
 {
 	int error;
-	git_oid old_id = {{0}};
+	git_oid old_id;
 	git_reference *tmp = NULL, *head = NULL, *peeled = NULL;
 	const char *name;
 
@@ -1148,7 +1148,8 @@
 		return 0;
 
 	/* if we can't resolve, we use {0}*40 as old id */
-	git_reference_name_to_id(&old_id, backend->repo, ref->name);
+	if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0)
+		memset(&old_id, 0, sizeof(old_id));
 
 	if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0)
 		return error;
diff --git a/src/refs.c b/src/refs.c
index 632a529..f7120d9 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -622,15 +622,25 @@
 static int update_wt_heads(git_repository *repo, const char *path, void *payload)
 {
 	rename_cb_data *data = (rename_cb_data *) payload;
-	git_reference *head;
+	git_reference *head = NULL;
 	char *gitdir = NULL;
-	int error = 0;
+	int error;
 
-	if (git_reference__read_head(&head, repo, path) < 0 ||
-	    git_reference_type(head) != GIT_REF_SYMBOLIC ||
-	    git__strcmp(head->target.symbolic, data->old_name) != 0 ||
-	    (gitdir = git_path_dirname(path)) == NULL)
+	if ((error = git_reference__read_head(&head, repo, path)) < 0) {
+		giterr_set(GITERR_REFERENCE, "could not read HEAD when renaming references");
 		goto out;
+	}
+
+	if ((gitdir = git_path_dirname(path)) == NULL) {
+		error = -1;
+		goto out;
+	}
+
+	if (git_reference_type(head) != GIT_REF_SYMBOLIC ||
+	    git__strcmp(head->target.symbolic, data->old_name) != 0) {
+		error = 0;
+		goto out;
+	}
 
 	/* Update HEAD it was pointing to the reference being renamed */
 	if ((error = git_repository_create_head(gitdir, data->new_name)) < 0) {
diff --git a/src/worktree.c b/src/worktree.c
index f224b23..ede155b 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -212,7 +212,7 @@
 		goto out;
 
 out:
-	free(name);
+	git__free(name);
 	git_buf_free(&parent);
 
 	return error;