Skip to content

Commit 48c171d

Browse files
committed
find_hook(): refactor the STRIP_EXTENSION logic
When looking for a hook and not finding one, and when `STRIP_EXTENSION` is available (read: if we're on Windows and `.exe` is the required extension for executable programs), we want to look also for a hook with that extension. Previously, we added that handling into the conditional block that was meant to handle when no hook was found (possibly providing some advice for the user's benefit). If the hook with that file extension was found, we'd return early from that function instead of writing out said advice, of course. However, we're about to introduce a safety valve to prevent hooks from being run during a clone, to reduce the attack surface of bugs that allow writing files to be written into arbitrary locations. To prepare for that, refactor the logic to avoid the early return, by separating the `STRIP_EXTENSION` handling from the conditional block handling the case when no hook was found. This commit is best viewed with `--patience`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 31572dc commit 48c171d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

hook.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ const char *find_hook(const char *name)
77
{
88
static struct strbuf path = STRBUF_INIT;
99

10+
int found_hook;
11+
1012
strbuf_reset(&path);
1113
strbuf_git_path(&path, "hooks/%s", name);
12-
if (access(path.buf, X_OK) < 0) {
14+
found_hook = access(path.buf, X_OK) >= 0;
15+
#ifdef STRIP_EXTENSION
16+
if (!found_hook) {
1317
int err = errno;
1418

15-
#ifdef STRIP_EXTENSION
1619
strbuf_addstr(&path, STRIP_EXTENSION);
17-
if (access(path.buf, X_OK) >= 0)
18-
return path.buf;
19-
if (errno == EACCES)
20-
err = errno;
20+
found_hook = access(path.buf, X_OK) >= 0;
21+
if (!found_hook)
22+
errno = err;
23+
}
2124
#endif
2225

23-
if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
26+
if (!found_hook) {
27+
if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
2428
static struct string_list advise_given = STRING_LIST_INIT_DUP;
2529

2630
if (!string_list_lookup(&advise_given, name)) {

0 commit comments

Comments
 (0)