From ce9dee4b2eb11d1a659d4ef595c4e699285882c0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 24 Nov 2017 00:29:04 -0800 Subject: [PATCH] posixtm: remove PDS_LEADING_YEAR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes the API slightly, in a hopefully-innocuous way. Without this change the code had undefined behavior when a caller specified neither PDS_LEADING_YEAR nor PDS_TRAILING_YEAR. Problem reported by Pádraig Brady in: https://lists.gnu.org/r/bug-gnulib/2017-11/msg00048.html * NEWS: Mention this. * lib/posixtm.c (posix_time_parse): Treat the absence of PDS_TRAILING_YEAR as if PDS_LEADING_YEAR were present. * lib/posixtm.h (PDS_LEADING_YEAR): Remove (actually, leave it present, but define it as zero, for compatibility with existing source code). All other PDS_* values moved up. * tests/test-posixtm.c (LY): New macro. (T): Use it. Do not expect a particular numeric encoding for PDS_CENTURY etc. --- ChangeLog | 18 +++++++++++ NEWS | 6 ++++ lib/posixtm.c | 4 +-- lib/posixtm.h | 13 +++++--- tests/test-posixtm.c | 90 +++++++++++++++++++++++++++------------------------- 5 files changed, 80 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index 700ee09..5152abb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2017-11-24 Paul Eggert + + posixtm: remove PDS_LEADING_YEAR + This changes the API slightly, in a hopefully-innocuous way. + Without this change the code had undefined behavior when a + caller specified neither PDS_LEADING_YEAR nor PDS_TRAILING_YEAR. + Problem reported by Pádraig Brady in: + https://lists.gnu.org/r/bug-gnulib/2017-11/msg00048.html + * NEWS: Mention this. + * lib/posixtm.c (posix_time_parse): Treat the absence of + PDS_TRAILING_YEAR as if PDS_LEADING_YEAR were present. + * lib/posixtm.h (PDS_LEADING_YEAR): Remove (actually, leave it + present, but define it as zero, for compatibility with existing + source code). All other PDS_* values moved up. + * tests/test-posixtm.c (LY): New macro. + (T): Use it. Do not expect a particular numeric encoding + for PDS_CENTURY etc. + 2017-11-23 Paul Eggert stat: work around Solaris bug with tv_nsec < 0 diff --git a/NEWS b/NEWS index d3a300a..9ec3a15 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,12 @@ User visible incompatible changes Date Modules Changes +2017-11-24 posixtm Previously, callers had to specify either + PDS_LEADING_YEAR or PDS_TRAILING_YEAR (but + not both). Now, callers should specify + only PDS_TRAILING_YEAR; leading years are + requested by not specifying PDS_TRAILING_YEAR. + 2017-08-14 fcntl-h This module now defaults O_CLOEXEC to a nonzero value instead of to 0, as the 'open' and 'openat' modules now emulate O_CLOEXEC. diff --git a/lib/posixtm.c b/lib/posixtm.c index 030f704..e718664 100644 --- a/lib/posixtm.c +++ b/lib/posixtm.c @@ -45,7 +45,7 @@ touch -t [[CC]YY]mmddhhmm[.ss] FILE... 8, 10, or 12 digits, followed by optional .ss - (PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS) + (PDS_CENTURY | PDS_SECONDS) touch mmddhhmm[YY] FILE... (obsoleted by POSIX 1003.1-2001) 8 or 10 digits, YY (if present) must be in the range 69-99 @@ -136,7 +136,7 @@ posix_time_parse (struct tm *tm, const char *s, unsigned int syntax_bits) pair[i] = 10 * (s[2*i] - '0') + s[2*i + 1] - '0'; p = pair; - if (syntax_bits & PDS_LEADING_YEAR) + if (! (syntax_bits & PDS_TRAILING_YEAR)) { if (! year (tm, p, len - 4, syntax_bits)) return false; diff --git a/lib/posixtm.h b/lib/posixtm.h index 090c15b..7674227 100644 --- a/lib/posixtm.h +++ b/lib/posixtm.h @@ -25,11 +25,14 @@ # include /* POSIX Date Syntax flags. */ -# define PDS_LEADING_YEAR 1 -# define PDS_TRAILING_YEAR 2 -# define PDS_CENTURY 4 -# define PDS_SECONDS 8 -# define PDS_PRE_2000 16 +# define PDS_TRAILING_YEAR 1 +# define PDS_CENTURY 2 +# define PDS_SECONDS 4 +# define PDS_PRE_2000 8 + +/* For compatibility with older versions of this header, in which + PDS_LEADING_YEAR had its own bit. */ +# define PDS_LEADING_YEAR 0 bool posixtime (time_t *p, const char *s, unsigned int syntax_bits); diff --git a/tests/test-posixtm.c b/tests/test-posixtm.c index 8b9078a..a9b37f8 100644 --- a/tests/test-posixtm.c +++ b/tests/test-posixtm.c @@ -37,67 +37,69 @@ struct posixtm_test int_least64_t t_expected; }; -/* Test mainly with syntax_bits == 13 - (aka: (PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS)) */ +/* Test mainly with syntax_bits == LY. */ +enum { LY = PDS_CENTURY | PDS_SECONDS }; static struct posixtm_test const T[] = { /* no year specified; cross-check via another posixtime call */ - { "12131415.16", 13, 1, 0}, /* ??? Dec 13 14:15:16 ???? */ - { "12131415", 13, 1, 0}, /* ??? Dec 13 14:15:00 ???? */ + { "12131415.16", LY, 1, 0}, /* ??? Dec 13 14:15:16 ???? */ + { "12131415", LY, 1, 0}, /* ??? Dec 13 14:15:00 ???? */ /* These two tests fail on 64-bit Solaris up through at least Solaris 10, which is off by one day for timestamps before 0001-01-01 00:00:00 UTC. */ - { "000001010000.00", 13, 1, + { "000001010000.00", LY, 1, - INT64_C (62167219200)},/* Sat Jan 1 00:00:00 0 */ - { "000012312359.59", 13, 1, + { "000012312359.59", LY, 1, - INT64_C (62135596801)},/* Fri Dec 31 23:59:59 0 */ - { "000101010000.00", 13, 1, + { "000101010000.00", LY, 1, - INT64_C (62135596800)},/* Sat Jan 1 00:00:00 1 */ - { "190112132045.51", 13, 1, + { "190112132045.51", LY, 1, - INT64_C (2147483649)},/* Fri Dec 13 20:45:51 1901 */ - { "190112132045.52", 13, 1, + { "190112132045.52", LY, 1, - INT64_C (2147483648)},/* Fri Dec 13 20:45:52 1901 */ - { "190112132045.53", 13, 1, -2147483647}, /* Fri Dec 13 20:45:53 1901 */ - { "190112132046.52", 13, 1, -2147483588}, /* Fri Dec 13 20:46:52 1901 */ - { "190112132145.52", 13, 1, -2147480048}, /* Fri Dec 13 21:45:52 1901 */ - { "190112142045.52", 13, 1, -2147397248}, /* Sat Dec 14 20:45:52 1901 */ - { "190201132045.52", 13, 1, -2144805248}, /* Mon Jan 13 20:45:52 1902 */ - { "196912312359.59", 13, 1, -1}, /* Wed Dec 31 23:59:59 1969 */ - { "197001010000.00", 13, 1, 0}, /* Thu Jan 1 00:00:00 1970 */ - { "197001010000.01", 13, 1, 1}, /* Thu Jan 1 00:00:01 1970 */ - { "197001010001.00", 13, 1, 60}, /* Thu Jan 1 00:01:00 1970 */ - { "197001010000.60", 13, 1, 60}, /* Thu Jan 1 00:01:00 1970 */ - { "197001010100.00", 13, 1, 3600}, /* Thu Jan 1 01:00:00 1970 */ - { "197001020000.00", 13, 1, 86400}, /* Fri Jan 2 00:00:00 1970 */ - { "197002010000.00", 13, 1, 2678400}, /* Sun Feb 1 00:00:00 1970 */ - { "197101010000.00", 13, 1, 31536000}, /* Fri Jan 1 00:00:00 1971 */ - { "197001000000.00", 13, 0, 0}, /* -- */ - { "197000010000.00", 13, 0, 0}, /* -- */ - { "197001010060.00", 13, 0, 0}, /* -- */ - { "197001012400.00", 13, 0, 0}, /* -- */ - { "197001320000.00", 13, 0, 0}, /* -- */ - { "197013010000.00", 13, 0, 0}, /* -- */ - { "203801190314.06", 13, 1, 2147483646}, /* Tue Jan 19 03:14:06 2038 */ - { "203801190314.07", 13, 1, 2147483647}, /* Tue Jan 19 03:14:07 2038 */ - { "203801190314.08", 13, 1, + { "190112132045.53", LY, 1, -2147483647}, /* Fri Dec 13 20:45:53 1901 */ + { "190112132046.52", LY, 1, -2147483588}, /* Fri Dec 13 20:46:52 1901 */ + { "190112132145.52", LY, 1, -2147480048}, /* Fri Dec 13 21:45:52 1901 */ + { "190112142045.52", LY, 1, -2147397248}, /* Sat Dec 14 20:45:52 1901 */ + { "190201132045.52", LY, 1, -2144805248}, /* Mon Jan 13 20:45:52 1902 */ + { "196912312359.59", LY, 1, -1}, /* Wed Dec 31 23:59:59 1969 */ + { "197001010000.00", LY, 1, 0}, /* Thu Jan 1 00:00:00 1970 */ + { "197001010000.01", LY, 1, 1}, /* Thu Jan 1 00:00:01 1970 */ + { "197001010001.00", LY, 1, 60}, /* Thu Jan 1 00:01:00 1970 */ + { "197001010000.60", LY, 1, 60}, /* Thu Jan 1 00:01:00 1970 */ + { "197001010100.00", LY, 1, 3600}, /* Thu Jan 1 01:00:00 1970 */ + { "197001020000.00", LY, 1, 86400}, /* Fri Jan 2 00:00:00 1970 */ + { "197002010000.00", LY, 1, 2678400}, /* Sun Feb 1 00:00:00 1970 */ + { "197101010000.00", LY, 1, 31536000}, /* Fri Jan 1 00:00:00 1971 */ + { "197001000000.00", LY, 0, 0}, /* -- */ + { "197000010000.00", LY, 0, 0}, /* -- */ + { "197001010060.00", LY, 0, 0}, /* -- */ + { "197001012400.00", LY, 0, 0}, /* -- */ + { "197001320000.00", LY, 0, 0}, /* -- */ + { "197013010000.00", LY, 0, 0}, /* -- */ + { "203801190314.06", LY, 1, 2147483646}, /* Tue Jan 19 03:14:06 2038 */ + { "203801190314.07", LY, 1, 2147483647}, /* Tue Jan 19 03:14:07 2038 */ + { "203801190314.08", LY, 1, INT64_C ( 2147483648)},/* Tue Jan 19 03:14:08 2038 */ - { "999912312359.59", 13, 1, + { "999912312359.59", LY, 1, INT64_C (253402300799)},/* Fri Dec 31 23:59:59 9999 */ - { "1112131415", 13, 1, 1323785700}, /* Tue Dec 13 14:15:00 2011 */ - { "1112131415.16", 13, 1, 1323785716}, /* Tue Dec 13 14:15:16 2011 */ - { "201112131415.16", 13, 1, 1323785716}, /* Tue Dec 13 14:15:16 2011 */ - { "191112131415.16", 13, 1, -1831974284}, /* Wed Dec 13 14:15:16 1911 */ - { "203712131415.16", 13, 1, 2144326516}, /* Sun Dec 13 14:15:16 2037 */ - { "3712131415.16", 13, 1, 2144326516}, /* Sun Dec 13 14:15:16 2037 */ - { "6812131415.16", 13, 1, + { "1112131415", LY, 1, 1323785700}, /* Tue Dec 13 14:15:00 2011 */ + { "1112131415.16", LY, 1, 1323785716}, /* Tue Dec 13 14:15:16 2011 */ + { "201112131415.16", LY, 1, 1323785716}, /* Tue Dec 13 14:15:16 2011 */ + { "191112131415.16", LY, 1, -1831974284}, /* Wed Dec 13 14:15:16 1911 */ + { "203712131415.16", LY, 1, 2144326516}, /* Sun Dec 13 14:15:16 2037 */ + { "3712131415.16", LY, 1, 2144326516}, /* Sun Dec 13 14:15:16 2037 */ + { "6812131415.16", LY, 1, INT64_C ( 3122633716)},/* Thu Dec 13 14:15:16 2068 */ - { "6912131415.16", 13, 1, -1590284}, /* Sat Dec 13 14:15:16 1969 */ - { "7012131415.16", 13, 1, 29945716}, /* Sun Dec 13 14:15:16 1970 */ - { "1213141599", 2, 1, 945094500}, /* Mon Dec 13 14:15:00 1999 */ - { "1213141500", 2, 1, 976716900}, /* Wed Dec 13 14:15:00 2000 */ + { "6912131415.16", LY, 1, -1590284}, /* Sat Dec 13 14:15:16 1969 */ + { "7012131415.16", LY, 1, 29945716}, /* Sun Dec 13 14:15:16 1970 */ + { "1213141599", PDS_TRAILING_YEAR, + 1, 945094500}, /* Mon Dec 13 14:15:00 1999 */ + { "1213141500", PDS_TRAILING_YEAR, + 1, 976716900}, /* Wed Dec 13 14:15:00 2000 */ { NULL, 0, 0, 0} }; -- 2.7.4