Patchwork Use TIME_T_MAX and TIME_T_MIN in tst-mktime2.c

login
register
mail settings
Submitter H.J. Lu
Date Jan. 14, 2016, 11:19 p.m.
Message ID <CAMe9rOqo0NLPq9=JxhKoS2NNS87WnOHMm2K8zykiovRO_hf9mA@mail.gmail.com>
Download mbox | patch
Permalink /patch/10388/
State New
Headers show

Comments

H.J. Lu - Jan. 14, 2016, 11:19 p.m.
On Thu, Jan 14, 2016 at 12:59 PM, Paul Eggert <eggert@cs.ucla.edu> wrote:
> On 01/14/2016 11:44 AM, H.J. Lu wrote:
>>
>> +#ifndef TIME_T_MIN
>> +# define TIME_T_MIN TYPE_MINIMUM (time_t)
>> +#endif
>> +#ifndef TIME_T_MAX
>> +# define TIME_T_MAX TYPE_MAXIMUM (time_t)
>> +#endif
>> +
>>   static time_t time_t_max;
>>   static time_t time_t_min;
>
>
> Please get rid of the lower-case variables, and replace all uses of them
> with the new (upper-case) macros.

Done.  OK for trunk?
Paul Eggert - Jan. 14, 2016, 11:45 p.m.
Thanks, that looks good for trunk.
Adhemerval Zanella Netto - Jan. 15, 2016, 12:18 a.m.
On 14-01-2016 21:45, Paul Eggert wrote:
> Thanks, that looks good for trunk.

LGTM, thanks.

Patch

From 5882b44fe08566fc92a086c1a58752dd729c131c Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 14 Jan 2016 04:15:03 -0800
Subject: [PATCH] Use TIME_T_MAX and TIME_T_MIN in tst-mktime2.c

GCC 5.3 compiles

for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
    continue;

into an infinite loop with -Os.  We can copy TIME_T_MAX and TIME_T_MIN
from time/mktime.c.

	[BZ #19466]
	* time/tst-mktime2.c (time_t_max): Removed.
	(time_t_min): Likewise.
	(TYPE_SIGNED): New.
	(TYPE_MINIMUM): Likewise.
	(TYPE_MAXIMUM): Likewise.
	(TIME_T_MIN): Likewise.
	(TIME_T_MAX): Likewise.
	(mktime_test): Replace time_t_max and time_t_min with TIME_T_MAX
	and TIME_T_MIN.
	(do_test): Likewise.
---
 time/tst-mktime2.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/time/tst-mktime2.c b/time/tst-mktime2.c
index bc7cc58..c41db3e 100644
--- a/time/tst-mktime2.c
+++ b/time/tst-mktime2.c
@@ -5,8 +5,28 @@ 
 #include <stdlib.h>
 #include <unistd.h>
 
-static time_t time_t_max;
-static time_t time_t_min;
+/* True if the arithmetic type T is signed.  */
+#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
+
+/* The maximum and minimum values for the integer type T.  These
+   macros have undefined behavior if T is signed and has padding bits.
+   If this is a problem for you, please let us know how to fix it for
+   your host.  */
+#define TYPE_MINIMUM(t) \
+  ((t) (! TYPE_SIGNED (t) \
+	? (t) 0 \
+	: ~ TYPE_MAXIMUM (t)))
+#define TYPE_MAXIMUM(t) \
+  ((t) (! TYPE_SIGNED (t) \
+	? (t) -1 \
+	: ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
+
+#ifndef TIME_T_MIN
+# define TIME_T_MIN TYPE_MINIMUM (time_t)
+#endif
+#ifndef TIME_T_MAX
+# define TIME_T_MAX TYPE_MAXIMUM (time_t)
+#endif
 
 /* Values we'll use to set the TZ environment variable.  */
 static const char *tz_strings[] =
@@ -53,8 +73,8 @@  static void
 mktime_test (time_t now)
 {
   mktime_test1 (now);
-  mktime_test1 ((time_t) (time_t_max - now));
-  mktime_test1 ((time_t) (time_t_min + now));
+  mktime_test1 ((time_t) (TIME_T_MAX - now));
+  mktime_test1 ((time_t) (TIME_T_MIN + now));
 }
 
 static void
@@ -113,19 +133,13 @@  do_test (void)
      isn't worth using anyway.  */
   alarm (60);
 
-  for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
-    continue;
-  time_t_max--;
-  if ((time_t) -1 < 0)
-    for (time_t_min = -1; (time_t) (time_t_min * 2) < 0; time_t_min *= 2)
-      continue;
-  delta = time_t_max / 997; /* a suitable prime number */
+  delta = TIME_T_MAX / 997; /* a suitable prime number */
   for (i = 0; i < N_STRINGS; i++)
     {
       if (tz_strings[i])
 	setenv ("TZ", tz_strings[i], 1);
 
-      for (t = 0; t <= time_t_max - delta; t += delta)
+      for (t = 0; t <= TIME_T_MAX - delta; t += delta)
 	mktime_test (t);
       mktime_test ((time_t) 1);
       mktime_test ((time_t) (60 * 60));
-- 
2.5.0