Skip to content

Commit fe12d8e

Browse files
René Scharfegitster
authored andcommitted
archive: simplify archive format guessing
The code to guess an output archive's format consumed any --format options and built a new one. Jonathan noticed that it does so in an unsafe way, risking to overflow the static buffer fmt_opt. Change the code to keep the existing --format options intact and to only add a new one if a format could be guessed based on the output file name. The new option is added as the first one, allowing the existing ones to overrule it, i.e. explicit --format options given on the command line win over format guesses, as before. To simplify the code further, format_from_name() is changed to return the full --format option, thus no potentially dangerous sprintf() calls are needed any more. Reported-by: Jonathan Nieder <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2b26e0e commit fe12d8e

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

builtin-archive.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static const char *format_from_name(const char *filename)
6767
return NULL;
6868
ext++;
6969
if (!strcasecmp(ext, "zip"))
70-
return "zip";
70+
return "--format=zip";
7171
return NULL;
7272
}
7373

@@ -81,41 +81,39 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
8181
const char *exec = "git-upload-archive";
8282
const char *output = NULL;
8383
const char *remote = NULL;
84-
const char *format = NULL;
84+
const char *format_option = NULL;
8585
struct option local_opts[] = {
8686
OPT_STRING('o', "output", &output, "file",
8787
"write the archive to this file"),
8888
OPT_STRING(0, "remote", &remote, "repo",
8989
"retrieve the archive from remote repository <repo>"),
9090
OPT_STRING(0, "exec", &exec, "cmd",
9191
"path to the remote git-upload-archive command"),
92-
OPT_STRING(0, "format", &format, "fmt", "archive format"),
9392
OPT_END()
9493
};
95-
char fmt_opt[32];
9694

9795
argc = parse_options(argc, argv, prefix, local_opts, NULL,
9896
PARSE_OPT_KEEP_ALL);
9997

10098
if (output) {
10199
create_output_file(output);
102-
if (!format)
103-
format = format_from_name(output);
100+
format_option = format_from_name(output);
104101
}
105102

106-
if (format) {
107-
sprintf(fmt_opt, "--format=%s", format);
108-
/*
109-
* We have enough room in argv[] to muck it in place,
110-
* because either --format and/or --output must have
111-
* been given on the original command line if we get
112-
* to this point, and parse_options() must have eaten
113-
* it, i.e. we can add back one element to the array.
114-
* But argv[] may contain "--"; we should make it the
115-
* first option.
116-
*/
103+
/*
104+
* We have enough room in argv[] to muck it in place, because
105+
* --output must have been given on the original command line
106+
* if we get to this point, and parse_options() must have eaten
107+
* it, i.e. we can add back one element to the array.
108+
*
109+
* We add a fake --format option at the beginning, with the
110+
* format inferred from our output filename. This way explicit
111+
* --format options can override it, and the fake option is
112+
* inserted before any "--" that might have been given.
113+
*/
114+
if (format_option) {
117115
memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
118-
argv[1] = fmt_opt;
116+
argv[1] = format_option;
119117
argv[++argc] = NULL;
120118
}
121119

t/t5000-tar-tree.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ test_expect_success 'git archive --format=zip with --output' \
189189
'git archive --format=zip --output=d2.zip HEAD &&
190190
test_cmp d.zip d2.zip'
191191

192+
test_expect_success 'git archive with --output, inferring format' '
193+
git archive --output=d3.zip HEAD &&
194+
test_cmp d.zip d3.zip
195+
'
196+
197+
test_expect_success 'git archive with --output, override inferred format' '
198+
git archive --format=tar --output=d4.zip HEAD &&
199+
test_cmp b.tar d4.zip
200+
'
201+
192202
$UNZIP -v >/dev/null 2>&1
193203
if [ $? -eq 127 ]; then
194204
say "Skipping ZIP tests, because unzip was not found"

0 commit comments

Comments
 (0)