Limit filename for format-patch
Badly formatted commits may have very long comments. This causes git-format-patch to fail. To avoid that, truncate the filename to a value we believe will always work. Err out if the patch file cannot be created. Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Robin Rosenberg committed
Feb 23, 2007 at 23:27 UTC
c06d2daa12f72f175e3569bbb2a5f25e5f134f5e
1 file changed
+21
-7
builtin-log.c
+21
-7
@@ -224,6 +224,9 @@ int cmd_log(int argc, const char **argv, const char *prefix)
224
return cmd_log_walk(&rev);
225
}
226
227
+/* format-patch */
228
+#define FORMAT_PATCH_NAME_MAX 64
229
+
230
static int istitlechar(char c)
231
{
232
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
@@ -264,15 +267,18 @@ static int git_format_config(const char *var, const char *value)
267
static FILE *realstdout = NULL;
268
static const char *output_directory = NULL;
269
267
-static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
270
+static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
271
{
269
- char filename[1024];
272
+ char filename[PATH_MAX];
273
char *sol;
274
int len = 0;
272
- int suffix_len = strlen(fmt_patch_suffix) + 10; /* ., NUL and slop */
275
+ int suffix_len = strlen(fmt_patch_suffix) + 1;
276
277
if (output_directory) {
275
- strlcpy(filename, output_directory, 1000);
278
+ if (strlen(output_directory) >=
279
+ sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
280
+ return error("name of output directory is too long");
281
+ strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
282
len = strlen(filename);
283
if (filename[len - 1] != '/')
284
filename[len++] = '/';
@@ -297,7 +303,8 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
303
}
304
305
for (j = 0;
300
- len < sizeof(filename) - suffix_len &&
306
+ j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
307
+ len < sizeof(filename) - suffix_len &&
308
sol[j] && sol[j] != '\n';
309
j++) {
310
if (istitlechar(sol[j])) {
@@ -314,10 +321,16 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
321
}
322
while (filename[len - 1] == '.' || filename[len - 1] == '-')
323
len--;
324
+ filename[len] = 0;
325
}
326
+ if (len + suffix_len >= sizeof(filename))
327
+ return error("Patch pathname too long");
328
strcpy(filename + len, fmt_patch_suffix);
329
fprintf(realstdout, "%s\n", filename);
320
- freopen(filename, "w", stdout);
330
+ if (freopen(filename, "w", stdout) == NULL)
331
+ return error("Cannot open patch file %s",filename);
332
+ return 0;
333
+
334
}
335
336
static int get_patch_id(struct commit *commit, struct diff_options *options,
@@ -573,7 +586,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
586
rev.message_id = message_id;
587
}
588
if (!use_stdout)
576
- reopen_stdout(commit, rev.nr, keep_subject);
589
+ if (reopen_stdout(commit, rev.nr, keep_subject))
590
+ die("Failed to create output files");
591
shown = log_tree_commit(&rev, commit);
592
free(commit->buffer);
593
commit->buffer = NULL;