summaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-07-07 17:56:10 +0200
committerVicent Marti <tanoku@gmail.com>2011-07-07 17:56:10 +0200
commitda5b1e1c730d580ed5d414860c84b67122564528 (patch)
tree688847b0111adc3c147eff640cff86ea345d046f /src/index.c
parent2b5af615e1f4344f6073d0ddf3e83256d9a8d58f (diff)
downloadlibgit2-da5b1e1c730d580ed5d414860c84b67122564528.tar.gz
index: Fix memory leak on OOM
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/index.c b/src/index.c
index 353c30025..986bcc0b9 100644
--- a/src/index.c
+++ b/src/index.c
@@ -380,8 +380,12 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
* replacing is not requested: just insert entry at the end;
* the index is no longer sorted
*/
- if (!replace)
- return git_vector_insert(&index->entries, entry);
+ if (!replace) {
+ if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
+ goto cleanup_oom;
+
+ return GIT_SUCCESS;
+ }
/* look if an entry with this path already exists */
position = git_index_find(index, source_entry->path);
@@ -390,8 +394,12 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
* if no entry exists add the entry at the end;
* the index is no longer sorted
*/
- if (position == GIT_ENOTFOUND)
- return git_vector_insert(&index->entries, entry);
+ if (position == GIT_ENOTFOUND) {
+ if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
+ goto cleanup_oom;
+
+ return GIT_SUCCESS;
+ }
/* exists, replace it */
entry_array = (git_index_entry **) index->entries.contents;
@@ -400,6 +408,11 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
entry_array[position] = entry;
return GIT_SUCCESS;
+
+cleanup_oom:
+ free((char *)entry->path);
+ free(entry);
+ return GIT_ENOMEM;;
}
static int index_init_entry(git_index_entry *entry, git_index *index, const char *rel_path, int stage)