Skip to content

Commit 03f2c77

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 470e28d commit 03f2c77

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

run-command.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,13 @@ int finish_async(struct async *async)
797797

798798
const char *find_hook(const char *name)
799799
{
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;
803801

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;
805807
}
806808

807809
int run_hook_ve(const char *const *env, const char *name, va_list args)

run-command.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ int start_command(struct child_process *);
5252
int finish_command(struct child_process *);
5353
int run_command(struct child_process *);
5454

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+
*/
5560
extern const char *find_hook(const char *name);
5661
LAST_ARG_MUST_BE_NULL
5762
extern int run_hook_le(const char *const *env, const char *name, ...);

0 commit comments

Comments
 (0)