Skip to content

Commit

Permalink
find_hook: keep our own static buffer
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Aug 10, 2015
1 parent 470e28d commit 03f2c77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,13 @@ int finish_async(struct async *async)

const char *find_hook(const char *name)
{
const char *path = git_path("hooks/%s", name);
if (access(path, X_OK) < 0)
path = NULL;
static struct strbuf path = STRBUF_INIT;

return path;
strbuf_reset(&path);
strbuf_git_path(&path, "hooks/%s", name);
if (access(path.buf, X_OK) < 0)
return NULL;
return path.buf;
}

int run_hook_ve(const char *const *env, const char *name, va_list args)
Expand Down
5 changes: 5 additions & 0 deletions run-command.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ int start_command(struct child_process *);
int finish_command(struct child_process *);
int run_command(struct child_process *);

/*
* Returns the path to the hook file, or NULL if the hook is missing
* or disabled. Note that this points to static storage that will be
* overwritten by further calls to find_hook and run_hook_*.
*/
extern const char *find_hook(const char *name);
LAST_ARG_MUST_BE_NULL
extern int run_hook_le(const char *const *env, const char *name, ...);
Expand Down

0 comments on commit 03f2c77

Please sign in to comment.