[前][次][番号順一覧][スレッド一覧]

ruby-changes:68472

From: Nobuyoshi <ko1@a...>
Date: Fri, 15 Oct 2021 01:26:23 +0900 (JST)
Subject: [ruby-changes:68472] e057b9eea9 (master): Prefer the reentrant versions of gmtime and localtime

https://git.ruby-lang.org/ruby.git/commit/?id=e057b9eea9

From e057b9eea9021046a43dd59f45fe0cf34d69e8cb Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 14 Oct 2021 20:09:37 +0900
Subject: Prefer the reentrant versions of gmtime and localtime

---
 ext/socket/ancdata.c | 12 +++++++++---
 time.c               |  4 ++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index 4ec3f3d0a8..aa62cab0ec 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -851,6 +851,12 @@ anc_inspect_ipv6_pktinfo(int level, int type, VALUE data, VALUE ret) https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L851
 }
 #endif
 
+#ifdef HAVE_GMTIME_R
+# define LOCALTIME(time, tm) localtime_r(&(time), &(tm))
+#else
+# define LOCALTIME(time, tm) ((tm) = *localtime(&(time)))
+#endif
+
 #if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
 static int
 inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
@@ -862,7 +868,7 @@ inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret) https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L868
         char buf[32];
         memcpy((char*)&tv, RSTRING_PTR(data), sizeof(tv));
         time = tv.tv_sec;
-        tm = *localtime(&time);
+        LOCALTIME(time, tm);
         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
         rb_str_catf(ret, " %s.%06ld", buf, (long)tv.tv_usec);
         return 1;
@@ -882,7 +888,7 @@ inspect_timespec_as_abstime(int level, int optname, VALUE data, VALUE ret) https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L888
         struct tm tm;
         char buf[32];
         memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
-        tm = *localtime(&ts.tv_sec);
+        LOCALTIME(ts.tv_sec, tm);
         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
         rb_str_catf(ret, " %s.%09ld", buf, (long)ts.tv_nsec);
         return 1;
@@ -906,7 +912,7 @@ inspect_bintime_as_abstime(int level, int optname, VALUE data, VALUE ret) https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L912
 	uint64_t res_h, res_l;
         char buf[32];
         memcpy((char*)&bt, RSTRING_PTR(data), sizeof(bt));
-        tm = *localtime(&bt.sec);
+        LOCALTIME(bt.sec, tm);
         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
 
 	/* res_h = frac * 10**19 / 2**64 */
diff --git a/time.c b/time.c
index b16ff0a6f1..fee12d34ff 100644
--- a/time.c
+++ b/time.c
@@ -1169,7 +1169,11 @@ init_leap_second_info(void) https://github.com/ruby/ruby/blob/trunk/time.c#L1169
         struct vtm vtm;
         wideval_t timew;
         now = time(NULL);
+#ifdef HAVE_GMTIME_R
+        gmtime_r(&now, &result);
+#else
         gmtime(&now);
+#endif
         tm = gmtime_with_leapsecond(&now, &result);
         if (!tm) return;
         this_year = tm->tm_year;
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]