lib/timeutils.c:strxxx_iso: do not wrap tm_year

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>
This commit is contained in:
J William Piggott 2017-12-09 15:05:38 -05:00
parent ee475ab23e
commit 2d9a0b0adb
1 changed files with 5 additions and 4 deletions

View 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;
}