]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/bugreport.c: use thread-safe localtime_r()
authorTaylor Blau <me@ttaylorr.com>
Tue, 1 Dec 2020 00:30:06 +0000 (19:30 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Dec 2020 21:05:37 +0000 (13:05 -0800)
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 <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/bugreport.c

index 3ad4b9b62e8498c51d550358bc63724b74eabbce..ad3cc9c02f62ca85ef3f9b1b891334b3791fadbb 100644 (file)
@@ -125,6 +125,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
        struct strbuf report_path = STRBUF_INIT;
        int report = -1;
        time_t now = time(NULL);
+       struct tm tm;
        char *option_output = NULL;
        char *option_suffix = "%Y-%m-%d-%H%M";
        const char *user_relative_path = NULL;
@@ -147,7 +148,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
        strbuf_complete(&report_path, '/');
 
        strbuf_addstr(&report_path, "git-bugreport-");
-       strbuf_addftime(&report_path, option_suffix, localtime(&now), 0, 0);
+       strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0);
        strbuf_addstr(&report_path, ".txt");
 
        switch (safe_create_leading_directories(report_path.buf)) {