]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/timeutils.c:strxxx_iso: do not wrap tm_year
authorJ William Piggott <elseifthen@gmx.com>
Sat, 9 Dec 2017 20:05:38 +0000 (15:05 -0500)
committerJ William Piggott <elseifthen@gmx.com>
Sat, 9 Dec 2017 20:05:38 +0000 (15:05 -0500)
Year can wrap when adding the tm struct epoch during iso formatting:
hwclock --utc --noadjfile --predict --date '67768034678844900 seconds'
-2147481749-12-31 23:59:59.000000-05:00

Patched:
hwclock --utc --noadjfile --predict --date '67768034678844900 seconds'
2147485547-12-31 23:59:59.000000-05:00

Comparable to date(1):
date -Ins --date '67768034678844900 seconds'
2147485547-12-31T23:59:59,547886165-0500

Note: the 'seconds' argument is relative to the current time, so
      reaching this max year output is a moving target. The values shown
      above were invalid one second later. They then overflow tm_year
      upon conversion with localtime(3) and friends, which causes them
      to return NULL indicating that an error occurred.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
lib/timeutils.c

index dd763ecc38d562a013a504fb0c28f3b6be1bc88d..a7459d2b69823834529a1a7c83ea02cb4d6c66c8 100644 (file)
@@ -406,8 +406,9 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
        int len;
 
        if (flags & ISO_DATE) {
-               len = snprintf(p, bufsz, "%4d-%.2d-%.2d", tm->tm_year + 1900,
-                                               tm->tm_mon + 1, tm->tm_mday);
+               len = snprintf(p, bufsz, "%4ld-%.2d-%.2d",
+                              tm->tm_year + (long) 1900,
+                              tm->tm_mon + 1, tm->tm_mday);
                if (len < 0 || (size_t) len > bufsz)
                        return -1;
                bufsz -= len;
@@ -423,7 +424,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
 
        if (flags & ISO_TIME) {
                len = snprintf(p, bufsz, "%02d:%02d:%02d", tm->tm_hour,
-                                                tm->tm_min, tm->tm_sec);
+                              tm->tm_min, tm->tm_sec);
                if (len < 0 || (size_t) len > bufsz)
                        return -1;
                bufsz -= len;
@@ -451,7 +452,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
                int zmin  = abs(tmin % 60);
                len = snprintf(p, bufsz, "%+03d:%02d", zhour,zmin);
                if (len < 0 || (size_t) len > bufsz)
-               return -1;
+                       return -1;
        }
        return 0;
 }