Fix initialization & freeing of inexistent repos Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
diff --git a/src/fileops.c b/src/fileops.c
index ea9935a..0633f9b 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -74,8 +74,13 @@ int gitfo_isdir(const char *path)
stat_error = gitfo_stat(path, &st);
}
- return (stat_error == 0 && S_ISDIR(st.st_mode)) ?
- GIT_SUCCESS : GIT_ENOTFOUND;
+ if (stat_error < GIT_SUCCESS)
+ return GIT_ENOTFOUND;
+
+ if (!S_ISDIR(st.st_mode))
+ return GIT_ENOTFOUND;
+
+ return GIT_SUCCESS;
}
int gitfo_exists(const char *path)
diff --git a/src/odb.c b/src/odb.c
index 9dbe1cc..d54ad83 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -210,7 +210,8 @@ void git_odb_close(git_odb *db)
{
unsigned int i;
- assert(db);
+ if (db == NULL)
+ return;
for (i = 0; i < db->backends.length; ++i) {
git_odb_backend *b = git_vector_get(&db->backends, i);
diff --git a/src/repository.c b/src/repository.c
index f27f77c..098d098 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -103,7 +103,6 @@ static int assign_repository_folders(git_repository *repo,
if (git_dir == NULL || gitfo_isdir(git_dir) < GIT_SUCCESS)
return GIT_ENOTFOUND;
-
/* store GIT_DIR */
path_len = strlen(git_dir);
strcpy(path_aux, git_dir);
@@ -279,6 +278,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
if (error < GIT_SUCCESS)
goto cleanup;
+
error = git_odb_open(&repo->db, repo->path_odb);
if (error < GIT_SUCCESS)
goto cleanup;
@@ -296,7 +296,8 @@ void git_repository_free(git_repository *repo)
git_hashtable_iterator it;
git_object *object;
- assert(repo);
+ if (repo == NULL)
+ return;
free(repo->path_workdir);
free(repo->path_index);
@@ -310,8 +311,13 @@ void git_repository_free(git_repository *repo)
git_object_free(object);
git_hashtable_free(repo->objects);
- git_odb_close(repo->db);
- git_index_free(repo->index);
+
+ if (repo->db != NULL)
+ git_odb_close(repo->db);
+
+ if (repo->index != NULL)
+ git_index_free(repo->index);
+
free(repo);
}
@@ -511,7 +517,8 @@ int git_object_write(git_object *object)
void git_object_free(git_object *object)
{
- assert(object);
+ if (object == NULL)
+ return;
git_object__source_close(object);
git_hashtable_remove(object->repo->objects, &object->id);
diff --git a/src/revwalk.c b/src/revwalk.c
index b724f77..f2eaa71 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -77,6 +77,9 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
void git_revwalk_free(git_revwalk *walk)
{
+ if (walk == NULL)
+ return;
+
git_revwalk_reset(walk);
git_hashtable_free(walk->commits);
free(walk);
@@ -90,6 +93,8 @@ git_repository *git_revwalk_repository(git_revwalk *walk)
int git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode)
{
+ assert(walk);
+
if (walk->walking)
return GIT_EBUSY;
@@ -165,6 +170,7 @@ static git_revwalk_commit *insert_commit(git_revwalk *walk, git_commit *commit_o
int git_revwalk_push(git_revwalk *walk, git_commit *commit)
{
+ assert(walk && commit);
return insert_commit(walk, commit) ? GIT_SUCCESS : GIT_ENOMEM;
}
@@ -186,6 +192,8 @@ static void mark_uninteresting(git_revwalk_commit *commit)
int git_revwalk_hide(git_revwalk *walk, git_commit *commit)
{
git_revwalk_commit *hide;
+
+ assert(walk && commit);
hide = insert_commit(walk, commit);
if (hide == NULL)
@@ -216,6 +224,8 @@ git_commit *git_revwalk_next(git_revwalk *walk)
{
git_revwalk_commit *next;
+ assert(walk);
+
if (!walk->walking)
prepare_walk(walk);
@@ -234,6 +244,8 @@ void git_revwalk_reset(git_revwalk *walk)
git_hashtable_iterator it;
git_revwalk_commit *commit;
+ assert(walk);
+
git_hashtable_iterator_init(walk->commits, &it);
while ((commit = (git_revwalk_commit *)