find_hook: keep our own static buffer

The find_hook function returns the results of git_path, which is a static buffer shared by other path-related calls. Returning such a buffer is slightly dangerous, because it can be overwritten by seemingly unrelated functions. Let's at least keep our _own_ static buffer, so you can only get in trouble by calling find_hook in quick succession, which is less likely to happen and more obvious to notice. While we're at it, let's add some documentation of the function's limitations. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 10, 2015 at 05:37 UTC 03f2c7731b59de75357730bc511ffa8847e1fb81
2 files changed +11 -4
run-command.c
+6 -4
@@ -797,11 +797,13 @@ int finish_async(struct async *async)
797
798 const char *find_hook(const char *name)
799 {
800 - const char *path = git_path("hooks/%s", name);
801 - if (access(path, X_OK) < 0)
802 - path = NULL;
800 + static struct strbuf path = STRBUF_INIT;
801
804 - return path;
802 + strbuf_reset(&path);
803 + strbuf_git_path(&path, "hooks/%s", name);
804 + if (access(path.buf, X_OK) < 0)
805 + return NULL;
806 + return path.buf;
807 }
808
809 int run_hook_ve(const char *const *env, const char *name, va_list args)
run-command.h
+5
@@ -52,6 +52,11 @@ int start_command(struct child_process *);
52 int finish_command(struct child_process *);
53 int run_command(struct child_process *);
54
55 +/*
56 + * Returns the path to the hook file, or NULL if the hook is missing
57 + * or disabled. Note that this points to static storage that will be
58 + * overwritten by further calls to find_hook and run_hook_*.
59 + */
60 extern const char *find_hook(const char *name);
61 LAST_ARG_MUST_BE_NULL
62 extern int run_hook_le(const char *const *env, const char *name, ...);