Skip to content

Commit c1bb33c

Browse files
peffgitster
authored andcommitted
git_extract_argv0_path: do nothing without RUNTIME_PREFIX
When the RUNTIME_PREFIX compile-time knob isn't set, we never look at the argv0_path we extract. We can push its declaration inside the #ifdef to make it more clear that the extract code is effectively a noop. This also un-confuses leak-checking of the argv0_path variable when RUNTIME_PREFIX isn't set. The compiler is free to drop this static variable that we set but never look at (and "gcc -O2" does so). But the compiler still must call strbuf_detach(), since it doesn't know whether that function has side effects; it just throws away the result rather than putting it into the global. Leak-checkers which work by scanning the data segment for pointers to heap blocks would normally consider the block as reachable at program end. But if the compiler removes the variable entirely, there's nothing to find. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 39b2f6a commit c1bb33c

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

exec_cmd.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#define MAX_ARGS 32
66

77
static const char *argv_exec_path;
8-
static const char *argv0_path;
98

109
#ifdef RUNTIME_PREFIX
10+
static const char *argv0_path;
1111

1212
static const char *system_prefix(void)
1313
{
@@ -27,13 +27,31 @@ static const char *system_prefix(void)
2727
}
2828
return prefix;
2929
}
30+
31+
void git_extract_argv0_path(const char *argv0)
32+
{
33+
const char *slash;
34+
35+
if (!argv0 || !*argv0)
36+
return;
37+
38+
slash = find_last_dir_sep(argv0);
39+
40+
if (slash)
41+
argv0_path = xstrndup(argv0, slash - argv0);
42+
}
43+
3044
#else
3145

3246
static const char *system_prefix(void)
3347
{
3448
return PREFIX;
3549
}
3650

51+
void git_extract_argv0_path(const char *argv0)
52+
{
53+
}
54+
3755
#endif /* RUNTIME_PREFIX */
3856

3957
char *system_path(const char *path)
@@ -47,19 +65,6 @@ char *system_path(const char *path)
4765
return strbuf_detach(&d, NULL);
4866
}
4967

50-
void git_extract_argv0_path(const char *argv0)
51-
{
52-
const char *slash;
53-
54-
if (!argv0 || !*argv0)
55-
return;
56-
57-
slash = find_last_dir_sep(argv0);
58-
59-
if (slash)
60-
argv0_path = xstrndup(argv0, slash - argv0);
61-
}
62-
6368
void git_set_argv_exec_path(const char *exec_path)
6469
{
6570
argv_exec_path = exec_path;

0 commit comments

Comments
 (0)