diff options
author | J William Piggott | 2017-12-09 21:05:38 +0100 |
---|---|---|
committer | J William Piggott | 2017-12-09 21:05:38 +0100 |
commit | 2d9a0b0adb509d568576ff51c6511d1eb21c8938 (patch) | |
tree | 4070de3d08442197c766f16c0142c594e7594642 /lib/timeutils.c | |
parent | lib/timeutils.c:strxxx_iso: test conversion errors (diff) | |
download | kernel-qcow2-util-linux-2d9a0b0adb509d568576ff51c6511d1eb21c8938.tar.gz kernel-qcow2-util-linux-2d9a0b0adb509d568576ff51c6511d1eb21c8938.tar.xz kernel-qcow2-util-linux-2d9a0b0adb509d568576ff51c6511d1eb21c8938.zip |
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>
Diffstat (limited to 'lib/timeutils.c')
-rw-r--r-- | lib/timeutils.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/timeutils.c b/lib/timeutils.c index dd763ecc3..a7459d2b6 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -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; } |