On Wed, Jun 11, 2014 at 1:38 PM, Jonas 'Sortie' Termansen
<sor...@maxsi.org> wrote:
> Hi Bruce,
>
> On 06/11/2014 02:38 AM, Bruce Korb wrote:
>> OK, I promise to fix it within the next 24 years.
>
> It will be a while before any 2038 issues happen, yeah.  Though, some
> operating systems vendors today are making guarantees that their current
> installations will be able to last past that point.
>
>> In truth, since this is a _duration_, it really constrains the
>> result to be within 68 years.
>
> Durations longer than 68 years would indeed be unusual.  (Though can
> this code be abused to parse absolute dates?)  Either way, this is a
> silly artificial restriction: The code takes great care not to overflow
> beyond the limit, but the limit isn't always right.  Jim points out the
> TYPE_MAXIMUM () macro from lib/intprops.h, it looks reliable and ideal
> for this purpose.  (It also avoids the problem yours had where
> right-shifts on negative signed integers can be either arithmetic or
> logical.)
>
> The TIME_MAX macro is currently just used twice and both uses are in the
> scale_n_add function. It would probably be sufficient to just discard
> the macro and instead use this declaration in scale_n_add:
>
>   time_t time_max = TYPE_MAXIMUM (time_t);
>
> That should be clean, simple and reliable - and make everyone happy. :-)

I wrote the patch, and tested by running this:

  ./gnulib-tool --create-testdir --dir=/tmp/x --with-tests --test parse-duration
From afa7c4a28feaac74b7d64b22c61a73377a9c0f2a Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyer...@fb.com>
Date: Wed, 11 Jun 2014 15:43:37 -0700
Subject: [PATCH] parse-duration: eliminate 31/31-bit time_t limitation

* lib/parse-duration.c: Include "intprops.h".
(TIME_MAX): Define to TYPE_MAXIMUM (time_t), thus
eliminating a limitation that the result duration could
not exceed 68 years.
* modules/parse-duration (Depends-on): Add intprops.
Reported by Jonas 'Sortie' Termansen.
---
 ChangeLog              | 10 ++++++++++
 lib/parse-duration.c   |  3 ++-
 modules/parse-duration |  1 +
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 53be01f..f34b3fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2014-06-11  Jim Meyering  <meyer...@fb.com>
+
+       parse-duration: eliminate 31/31-bit time_t limitation
+       * lib/parse-duration.c: Include "intprops.h".
+       (TIME_MAX): Define to TYPE_MAXIMUM (time_t), thus
+       eliminating a limitation that the result duration could
+       not exceed 68 years.
+       * modules/parse-duration (Depends-on): Add intprops.
+       Reported by Jonas 'Sortie' Termansen.
+
 2014-06-07  Paul Eggert  <egg...@cs.ucla.edu>

        maint: fix typo in fdl.texi
diff --git a/lib/parse-duration.c b/lib/parse-duration.c
index 037e81e..8c7d9d7 100644
--- a/lib/parse-duration.c
+++ b/lib/parse-duration.c
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "intprops.h"

 #ifndef NUL
 #define NUL '\0'
@@ -51,7 +52,7 @@ typedef enum {
 #define SEC_PER_MONTH   (SEC_PER_DAY * 30)
 #define SEC_PER_YEAR    (SEC_PER_DAY * 365)

-#define TIME_MAX        0x7FFFFFFF
+#define TIME_MAX        TYPE_MAXIMUM (time_t)

 /* Wrapper around strtoul that does not require a cast.  */
 static unsigned long
diff --git a/modules/parse-duration b/modules/parse-duration
index 46288c0..6184dc4 100644
--- a/modules/parse-duration
+++ b/modules/parse-duration
@@ -6,6 +6,7 @@ lib/parse-duration.h
 lib/parse-duration.c

 Depends-on:
+intprops

 configure.ac:

-- 
2.0.0.254.g50f84e3

Reply via email to