Skip to content

Commit 7915691

Browse files
peffgitster
authored andcommitted
builtins: annotate always-empty prefix parameters
It's usually a bad idea for a builtin's cmd_foo() to ignore the "prefix" argument it gets, as it needs to prepend that string when accessing any paths given by the user. But if a builtin does not ask for the git wrapper to run repository setup (via the RUN_SETUP or RUN_SETUP_GENTLY flags), then we know the prefix will always be NULL (it is adjusting for the chdir() done during repo setup, but there cannot be one if we did not set up the repo). In those cases it's OK to ignore "prefix", but it's worth annotating for a few reasons: 1. It serves as documentation to somebody reading the code about what we expect. 2. If the flags in git.c ever change, the run-time assertion may help detect the problem (though only if the command is run from a subdirectory of the repository). 3. It notes to the compiler that we are OK ignoring "prefix". In particular, this silences -Wunused-parameter. It _could_ also help the compiler generate better code (because it will know the prefix is NULL), but in practice this is quite unlikely to matter. Note that I've only added this annotation to commands which triggered -Wunused-parameter. It would be correct to add it to any builtin which doesn't ask for RUN_SETUP, but most of the rest of them do the sensible thing with "prefix" by passing it to parse_options(). So they're much more likely to just work if they ever switched to RUN_SETUP, and aren't worth annotating. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 836c8ce commit 7915691

File tree

7 files changed

+22
-0
lines changed

7 files changed

+22
-0
lines changed

builtin.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ void setup_auto_pager(const char *cmd, int def);
107107

108108
int is_builtin(const char *s);
109109

110+
/*
111+
* Builtins which do not use RUN_SETUP should never see
112+
* a prefix that is not empty; use this to protect downstream
113+
* code which is not prepared to call prefix_filename(), etc.
114+
*/
115+
#define BUG_ON_NON_EMPTY_PREFIX(prefix) do { \
116+
if ((prefix)) \
117+
BUG("unexpected prefix in builtin: %s", (prefix)); \
118+
} while (0)
119+
110120
int cmd_add(int argc, const char **argv, const char *prefix);
111121
int cmd_am(int argc, const char **argv, const char *prefix);
112122
int cmd_annotate(int argc, const char **argv, const char *prefix);

builtin/check-ref-format.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
6060
char *to_free = NULL;
6161
int ret = 1;
6262

63+
BUG_ON_NON_EMPTY_PREFIX(prefix);
64+
6365
if (argc == 2 && !strcmp(argv[1], "-h"))
6466
usage(builtin_check_ref_format_usage);
6567

builtin/get-tar-commit-id.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
2424
long len;
2525
char *end;
2626

27+
BUG_ON_NON_EMPTY_PREFIX(prefix);
28+
2729
if (argc != 1)
2830
usage(builtin_get_tar_commit_id_usage);
2931

builtin/mailsplit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix)
277277
const char **argp;
278278
static const char *stdin_only[] = { "-", NULL };
279279

280+
BUG_ON_NON_EMPTY_PREFIX(prefix);
281+
280282
for (argp = argv+1; *argp; argp++) {
281283
const char *arg = *argp;
282284

builtin/remote-ext.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ static int command_loop(const char *child)
197197

198198
int cmd_remote_ext(int argc, const char **argv, const char *prefix)
199199
{
200+
BUG_ON_NON_EMPTY_PREFIX(prefix);
201+
200202
if (argc != 3)
201203
usage(usage_msg);
202204

builtin/remote-fd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ int cmd_remote_fd(int argc, const char **argv, const char *prefix)
5959
int output_fd = -1;
6060
char *end;
6161

62+
BUG_ON_NON_EMPTY_PREFIX(prefix);
63+
6264
if (argc != 3)
6365
usage(usage_msg);
6466

builtin/upload-archive.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
7979
{
8080
struct child_process writer = CHILD_PROCESS_INIT;
8181

82+
BUG_ON_NON_EMPTY_PREFIX(prefix);
83+
8284
if (argc == 2 && !strcmp(argv[1], "-h"))
8385
usage(upload_archive_usage);
8486

0 commit comments

Comments
 (0)