All of lore.kernel.org
 help / color / mirror / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: git@vger.kernel.org
Cc: emilyshaffer@google.com
Subject: [PATCH v2] builtin/bugreport.c: use thread-safe localtime_r()
Date: Mon, 30 Nov 2020 19:30:06 -0500	[thread overview]
Message-ID: <73eb4965807ea2fdf94f815a8f8a2b036296ecca.1606782566.git.me@ttaylorr.com> (raw)
In-Reply-To: <27fc158339c91f56210f00dae9015da1d6c781ec.1606777520.git.me@ttaylorr.com>

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>
---
How embarrassing: I forgot my sign-off on the previous message. The
contents in this version are unchanged, but this one includes my
sign-off.

 builtin/bugreport.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index 3ad4b9b62e..ad3cc9c02f 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -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)) {
--
2.29.2.533.g07db1f5344

  reply	other threads:[~2020-12-01  0:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-30 23:06 [PATCH] builtin/bugreport.c: use thread-safe localtime_r() Taylor Blau
2020-12-01  0:30 ` Taylor Blau [this message]
2020-12-01  2:27   ` [PATCH v2] " Jeff King
2020-12-01  3:15     ` Eric Sunshine
2020-12-01 18:27     ` Junio C Hamano
2020-12-01 18:34       ` Taylor Blau
2020-12-01 21:11         ` [PATCH v2 1/2] banned.h: mark non-reentrant gmtime, etc as banned Junio C Hamano
2020-12-01 21:11           ` [PATCH v2 2/2] banned.h: mark ctime_r() and asctime_r() " Junio C Hamano
2020-12-01 21:16             ` Eric Sunshine
2020-12-01 22:07               ` Junio C Hamano
2020-12-01 22:22                 ` Taylor Blau
2020-12-06 14:56           ` [PATCH v2 1/2] banned.h: mark non-reentrant gmtime, etc " SZEDER Gábor
2020-12-02  1:57       ` [PATCH v2] builtin/bugreport.c: use thread-safe localtime_r() Jeff King
2020-12-01  0:31 ` [PATCH] " Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=73eb4965807ea2fdf94f815a8f8a2b036296ecca.1606782566.git.me@ttaylorr.com \
    --to=me@ttaylorr.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.