@matheusbsbs / matheus-bagliano / commits / fe12d8e84f
Fork 0

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 <jrnieder@gmail.com> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 8, 2010 at 00:30 UTC fe12d8e84f745303d64757307e9a6a81a6608018
2 files changed +26 -18
builtin-archive.c
+16 -18
@@ -67,7 +67,7 @@ static const char *format_from_name(const char *filename)
67 return NULL;
68 ext++;
69 if (!strcasecmp(ext, "zip"))
70 - return "zip";
70 + return "--format=zip";
71 return NULL;
72 }
73
@@ -81,7 +81,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
81 const char *exec = "git-upload-archive";
82 const char *output = NULL;
83 const char *remote = NULL;
84 - const char *format = NULL;
84 + const char *format_option = NULL;
85 struct option local_opts[] = {
86 OPT_STRING('o', "output", &output, "file",
87 "write the archive to this file"),
@@ -89,33 +89,31 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
89 "retrieve the archive from remote repository <repo>"),
90 OPT_STRING(0, "exec", &exec, "cmd",
91 "path to the remote git-upload-archive command"),
92 - OPT_STRING(0, "format", &format, "fmt", "archive format"),
92 OPT_END()
93 };
95 - char fmt_opt[32];
94
95 argc = parse_options(argc, argv, prefix, local_opts, NULL,
96 PARSE_OPT_KEEP_ALL);
97
98 if (output) {
99 create_output_file(output);
102 - if (!format)
103 - format = format_from_name(output);
100 + format_option = format_from_name(output);
101 }
102
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) {
115 memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
118 - argv[1] = fmt_opt;
116 + argv[1] = format_option;
117 argv[++argc] = NULL;
118 }
119
t/t5000-tar-tree.sh
+10
@@ -189,6 +189,16 @@ test_expect_success 'git archive --format=zip with --output' \
189 'git archive --format=zip --output=d2.zip HEAD &&
190 test_cmp d.zip d2.zip'
191
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 +
202 $UNZIP -v >/dev/null 2>&1
203 if [ $? -eq 127 ]; then
204 say "Skipping ZIP tests, because unzip was not found"