diff options
| author | Vicent Martà <vicent@github.com> | 2013-05-16 11:10:13 -0700 |
|---|---|---|
| committer | Vicent Martà <vicent@github.com> | 2013-05-16 11:10:13 -0700 |
| commit | 18e37db9359bc5e109702eec7422b95fac21b106 (patch) | |
| tree | 3f6dd8eebaa15ae90d8a01715ff974bec06b63a7 | |
| parent | 4a782c82efc17dc5ad52579783ef0d48c34c5330 (diff) | |
| parent | 57908bb3a3b3a0f3de75d13ef432e3964dab9212 (diff) | |
| download | libgit2-18e37db9359bc5e109702eec7422b95fac21b106.tar.gz | |
Merge pull request #1591 from arrbee/reorder-reuc-read-for-safety
Ensure reuc vector is always valid
| -rw-r--r-- | src/index.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/index.c b/src/index.c index f767dfab7..f7f7133d6 100644 --- a/src/index.c +++ b/src/index.c @@ -1283,8 +1283,9 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) size_t len; int i; - /* This gets called multiple times, the vector might already be initialized */ - if (index->reuc._alloc_size == 0 && git_vector_init(&index->reuc, 16, reuc_cmp) < 0) + /* If called multiple times, the vector might already be initialized */ + if (index->reuc._alloc_size == 0 && + git_vector_init(&index->reuc, 16, reuc_cmp) < 0) return -1; while (size) { @@ -1294,12 +1295,9 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) if (size <= len) return index_error_invalid("reading reuc entries"); - lost = git__malloc(sizeof(git_index_reuc_entry)); + lost = git__calloc(1, sizeof(git_index_reuc_entry)); GITERR_CHECK_ALLOC(lost); - if (git_vector_insert(&index->reuc, lost) < 0) - return -1; - /* read NUL-terminated pathname for entry */ lost->path = git__strdup(buffer); GITERR_CHECK_ALLOC(lost->path); @@ -1337,6 +1335,10 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) size -= 20; buffer += 20; } + + /* entry was read successfully - insert into reuc vector */ + if (git_vector_insert(&index->reuc, lost) < 0) + return -1; } /* entries are guaranteed to be sorted on-disk */ |
