From owner-svn-src-head@freebsd.org Wed Oct 17 14:51:44 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 1773510D722D; Wed, 17 Oct 2018 14:51:44 +0000 (UTC) (envelope-from yuripv@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C107282AC9; Wed, 17 Oct 2018 14:51:43 +0000 (UTC) (envelope-from yuripv@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A1FDE50A4; Wed, 17 Oct 2018 14:51:43 +0000 (UTC) (envelope-from yuripv@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w9HEphbP089643; Wed, 17 Oct 2018 14:51:43 GMT (envelope-from yuripv@FreeBSD.org) Received: (from yuripv@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w9HEph96089642; Wed, 17 Oct 2018 14:51:43 GMT (envelope-from yuripv@FreeBSD.org) Message-Id: <201810171451.w9HEph96089642@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: yuripv set sender to yuripv@FreeBSD.org using -f From: Yuri Pankov Date: Wed, 17 Oct 2018 14:51:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r339408 - head/lib/libc/stdtime X-SVN-Group: head X-SVN-Commit-Author: yuripv X-SVN-Commit-Paths: head/lib/libc/stdtime X-SVN-Commit-Revision: 339408 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Oct 2018 14:51:44 -0000 Author: yuripv Date: Wed Oct 17 14:51:43 2018 New Revision: 339408 URL: https://svnweb.freebsd.org/changeset/base/339408 Log: strptime: fix parsing of tm_year when both %C and %y appear in the format string in arbitrary order. This makes the related test cases in lib/libc/tests/time (not yet connected to the build) pass. While here, don't error on negative tm_year value based on the APPLICATION USAGE in http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html (glibc does the same): tm_year is a signed value; therefore, years before 1900 may be represented. Approved by: re (gjb), kib (mentor) Differential Revision: https://reviews.freebsd.org/D17550 Modified: head/lib/libc/stdtime/strptime.c Modified: head/lib/libc/stdtime/strptime.c ============================================================================== --- head/lib/libc/stdtime/strptime.c Wed Oct 17 10:31:08 2018 (r339407) +++ head/lib/libc/stdtime/strptime.c Wed Oct 17 14:51:43 2018 (r339408) @@ -95,6 +95,7 @@ _strptime(const char *buf, const char *fmt, struct tm int i, len; int flags; int Ealternative, Oalternative; + int century, year; const struct lc_time_T *tptr = __get_current_time_locale(locale); static int start_of_month[2][13] = { {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, @@ -102,6 +103,8 @@ _strptime(const char *buf, const char *fmt, struct tm }; flags = FLAG_NONE; + century = -1; + year = -1; ptr = fmt; while (*ptr != 0) { @@ -146,10 +149,8 @@ label: i += *buf - '0'; len--; } - if (i < 19) - return (NULL); - tm->tm_year = i * 100 - TM_YEAR_BASE; + century = i; flags |= FLAG_YEAR; break; @@ -527,13 +528,9 @@ label: len--; } if (c == 'Y') - i -= TM_YEAR_BASE; - if (c == 'y' && i < 69) - i += 100; - if (i < 0) - return (NULL); + century = i / 100; + year = i % 100; - tm->tm_year = i; flags |= FLAG_YEAR; break; @@ -609,6 +606,17 @@ label: default: return (NULL); } + } + + if (century != -1 || year != -1) { + if (year == -1) + year = 0; + if (century == -1) { + if (year < 69) + year += 100; + } else + year += century * 100 - TM_YEAR_BASE; + tm->tm_year = year; } if (!(flags & FLAG_YDAY) && (flags & FLAG_YEAR)) {