bugreport.c: fix a crash in `git bugreport` with `--no-suffix` option

`git bugreport` does not complain when `--no-suffix` is given, but it leads to a segmentation fault as the it is not prepared to see a NULL assigned to the option_suffix variable. Signed-off-by: Jiamu Sun <barroit@linux.com> Acked-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiamu Sun committed Mar 14, 2024 at 04:00 UTC b3b57c69dad91382ccc36ebf5d668454607dd568
2 files changed +12 -4
Documentation/git-bugreport.txt
+5 -1
@@ -8,7 +8,8 @@ git-bugreport - Collect information for user to file a bug report
8 SYNOPSIS
9 --------
10 [verse]
11 -'git bugreport' [(-o | --output-directory) <path>] [(-s | --suffix) <format>]
11 +'git bugreport' [(-o | --output-directory) <path>]
12 + [(-s | --suffix) <format> | --no-suffix]
13 [--diagnose[=<mode>]]
14
15 DESCRIPTION
@@ -51,9 +52,12 @@ OPTIONS
52
53 -s <format>::
54 --suffix <format>::
55 +--no-suffix::
56 Specify an alternate suffix for the bugreport name, to create a file
57 named 'git-bugreport-<formatted-suffix>'. This should take the form of a
58 strftime(3) format string; the current local time will be used.
59 + `--no-suffix` disables the suffix and the file is just named
60 + `git-bugreport` without any disambiguation measure.
61
62 --no-diagnose::
63 --diagnose[=<mode>]::
builtin/bugreport.c
+7 -3
@@ -64,7 +64,8 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
64 }
65
66 static const char * const bugreport_usage[] = {
67 - N_("git bugreport [(-o | --output-directory) <path>] [(-s | --suffix) <format>]\n"
67 + N_("git bugreport [(-o | --output-directory) <path>]\n"
68 + " [(-s | --suffix) <format> | --no-suffix]\n"
69 " [--diagnose[=<mode>]]"),
70 NULL
71 };
@@ -138,8 +139,11 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
139 strbuf_complete(&report_path, '/');
140 output_path_len = report_path.len;
141
141 - strbuf_addstr(&report_path, "git-bugreport-");
142 - strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0);
142 + strbuf_addstr(&report_path, "git-bugreport");
143 + if (option_suffix) {
144 + strbuf_addch(&report_path, '-');
145 + strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0);
146 + }
147 strbuf_addstr(&report_path, ".txt");
148
149 switch (safe_create_leading_directories(report_path.buf)) {