We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c
keeps a linked list of all created lock_file structures. So let's make the
'lock_file' variable a pointer to a 'struct lock_file'

As the same instance of this struct can be reused, let's add an argument
to init_apply_state(), so that the caller can supply the same instance to
different calls. And let's alloc an instance in init_apply_state(), if the
caller doesn't want to supply one.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 2aea8ba..cad2c56 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -52,6 +52,12 @@ struct apply_state {
        const char *prefix;
        int prefix_length;
 
+       /*
+        * Since lockfile.c keeps a linked list of all created
+        * lock_file structures, it isn't safe to free(lock_file).
+        */
+       struct lock_file *lock_file;
+
        int apply;
        int allow_overlap;
        int apply_in_reverse;
@@ -4504,8 +4510,6 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
        return errs;
 }
 
-static struct lock_file lock_file;
-
 #define INACCURATE_EOF (1<<0)
 #define RECOUNT                (1<<1)
 
@@ -4558,7 +4562,7 @@ static int apply_patch(struct apply_state *state,
 
        state->update_index = state->check_index && state->apply;
        if (state->update_index && newfd < 0)
-               newfd = hold_locked_index(&lock_file, 1);
+               newfd = hold_locked_index(state->lock_file, 1);
 
        if (state->check_index) {
                if (read_cache() < 0)
@@ -4659,11 +4663,14 @@ static int option_parse_directory(const struct option 
*opt,
        return 0;
 }
 
-static void init_apply_state(struct apply_state *state, const char *prefix)
+static void init_apply_state(struct apply_state *state,
+                            const char *prefix,
+                            struct lock_file *lock_file)
 {
        memset(state, 0, sizeof(*state));
        state->prefix = prefix;
        state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+       state->lock_file = lock_file ? lock_file : xcalloc(1, 
sizeof(*lock_file));
        state->apply = 1;
        state->line_termination = '\n';
        state->p_value = 1;
@@ -4771,7 +4778,7 @@ static int apply_all_patches(struct apply_state *state,
        }
 
        if (state->update_index) {
-               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+               if (write_locked_index(&the_index, state->lock_file, 
COMMIT_LOCK))
                        die(_("Unable to write new index file"));
        }
 
@@ -4853,7 +4860,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
                OPT_END()
        };
 
-       init_apply_state(&state, prefix);
+       init_apply_state(&state, prefix, NULL);
 
        argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
                        apply_usage, 0);
-- 
2.8.2.490.g3dabe57

--
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