On Tue, Jul 14, 2015 at 09:54:10AM -0700, Stefan Beller wrote:
> So what I meant to suggest, was to only allocate the memory if we really need 
> it
> by moving the allocation further down.
> 
> static int clean_index(const unsigned char *head, const unsigned char *remote)
> {
>        struct lock_file *lock_file;
> ...
> ... // includes return -1, which would not leak the memory already allocated
> ...
>        lock_file = xalloc (...);
>        hold_locked_index(lock_file, 1);

Ah, sorry, I misinterpreted your message ><. Thanks for catching this.

I've moved all the lock file memory allocation to just before the
hold_locked_index() calls on my end.

diff --git a/builtin/am.c b/builtin/am.c
index c597325..dc8e862 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1138,13 +1138,12 @@ static const char *msgnum(const struct am_state *state)
  */
 static void refresh_and_write_cache(void)
 {
-       static struct lock_file lock_file;
+       struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       hold_locked_index(&lock_file, 1);
+       hold_locked_index(lock_file, 1);
        refresh_cache(REFRESH_QUIET);
-       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+       if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
                die(_("unable to write index file"));
-       rollback_lock_file(&lock_file);
 }
 
 /**
@@ -1923,13 +1922,14 @@ next:
  */
 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
 {
-       struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
+       struct lock_file *lock_file;
        struct unpack_trees_options opts;
        struct tree_desc t[2];
 
        if (parse_tree(head) || parse_tree(remote))
                return -1;
 
+       lock_file = xcalloc(1, sizeof(struct lock_file));
        hold_locked_index(lock_file, 1);
 
        refresh_cache(REFRESH_QUIET);
@@ -1962,7 +1962,7 @@ static int fast_forward_to(struct tree *head, struct tree 
*remote, int reset)
  */
 static int clean_index(const unsigned char *head, const unsigned char *remote)
 {
-       struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
+       struct lock_file *lock_file;
        struct tree *head_tree, *remote_tree, *index_tree;
        unsigned char index[GIT_SHA1_RAWSZ];
        struct pathspec pathspec;
@@ -1992,6 +1992,7 @@ static int clean_index(const unsigned char *head, const 
unsigned char *remote)
 
        memset(&pathspec, 0, sizeof(pathspec));
 
+       lock_file = xcalloc(1, sizeof(struct lock_file));
        hold_locked_index(lock_file, 1);
 
        if (read_tree(remote_tree, 0, &pathspec)) {

Thanks,
Paul
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to