Skip to content

Commit 4f6460d

Browse files
ttaylorrgitster
authored andcommitted
builtin/bugreport.c: use thread-safe localtime_r()
To generate its filename, the 'git bugreport' builtin asks the system for the current time with 'localtime()'. Since this uses a shared buffer, it is not thread-safe. Even though 'git bugreport' is not multi-threaded, using localtime() can trigger some static analysis tools to complain, and a quick $ git grep -oh 'localtime\(_.\)\?' -- **/*.c | sort | uniq -c shows that the only usage of the thread-unsafe 'localtime' is in a piece of documentation. So, convert this instance to use the thread-safe version for consistency, and to appease some analysis tools. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 72ffeb9 commit 4f6460d

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

builtin/bugreport.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
125125
struct strbuf report_path = STRBUF_INIT;
126126
int report = -1;
127127
time_t now = time(NULL);
128+
struct tm tm;
128129
char *option_output = NULL;
129130
char *option_suffix = "%Y-%m-%d-%H%M";
130131
const char *user_relative_path = NULL;
@@ -147,7 +148,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
147148
strbuf_complete(&report_path, '/');
148149

149150
strbuf_addstr(&report_path, "git-bugreport-");
150-
strbuf_addftime(&report_path, option_suffix, localtime(&now), 0, 0);
151+
strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0);
151152
strbuf_addstr(&report_path, ".txt");
152153

153154
switch (safe_create_leading_directories(report_path.buf)) {

0 commit comments

Comments
 (0)