Use TIME_T_MAX and TIME_T_MIN in tst-mktime2.c

Message ID 20160114194439.GA25187@intel.com
State New, archived
Headers

Commit Message

Lu, Hongjiu Jan. 14, 2016, 7:44 p.m. UTC
  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.

Tested on x86-64.  OK for trunk?

H.J.
---
	[BZ #19466]
	* time/tst-mktime2.c (TYPE_SIGNED): New.
	(TYPE_MINIMUM): Likewise.
	(TYPE_MAXIMUM): Likewise.
	(TIME_T_MIN): Likewise.
	(TIME_T_MAX): Likewise.
	(do_test): Initialize time_t_max and time_t_min with TIME_T_MAX
	and TIME_T_MIN.
---
 time/tst-mktime2.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
  

Comments

Paul Eggert Jan. 14, 2016, 8:59 p.m. UTC | #1
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.
  

Patch

diff --git a/time/tst-mktime2.c b/time/tst-mktime2.c
index bc7cc58..fd40ab7 100644
--- a/time/tst-mktime2.c
+++ b/time/tst-mktime2.c
@@ -5,6 +5,29 @@ 
 #include <stdlib.h>
 #include <unistd.h>
 
+/* 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
+
 static time_t time_t_max;
 static time_t time_t_min;
 
@@ -113,12 +136,8 @@  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;
+  time_t_max = TIME_T_MAX;
+  time_t_min = TIME_T_MIN;
   delta = time_t_max / 997; /* a suitable prime number */
   for (i = 0; i < N_STRINGS; i++)
     {