From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id A081F3858C42; Wed, 21 Feb 2024 19:03:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A081F3858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1708542195; bh=7Jni/F9qDTQ+aZoWYrnsh4/JZXbD4EMBmoi1xEVRHH8=; h=From:To:Subject:Date:From; b=fz6IbLDZnjU1/T2sTLxz5yJyXDqOzXwR8MX/qPonbDh7PUkgMAP0Uwm7YB5HlNy1w vnHm8tblvQ8GO0MFh+n3HDifzIm25uREFtvzJHJ6QAqOpMARrOTnPW+7dMvTRsdGjZ z/mNjDJXrilsWYu1efuzodNUUoCao9mQoSdO1ivs= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_5-branch] Cygwin: strptime: fix am/pm handling X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/cygwin-3_5-branch X-Git-Oldrev: f0ab27c0954e9529109e00c905be2d1ea88a749a X-Git-Newrev: 4fe5b1d77bc60d2a44c7fcd9a0b7d66d0035cdea Message-Id: <20240221190315.A081F3858C42@sourceware.org> Date: Wed, 21 Feb 2024 19:03:15 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D4fe5b1d77bc= 60d2a44c7fcd9a0b7d66d0035cdea commit 4fe5b1d77bc60d2a44c7fcd9a0b7d66d0035cdea Author: Corinna Vinschen AuthorDate: Wed Feb 21 19:54:20 2024 +0100 Commit: Corinna Vinschen CommitDate: Wed Feb 21 20:00:59 2024 +0100 Cygwin: strptime: fix am/pm handling =20 The %p format specifier is handled immediately. It requires that tm_hour is already set. This falls flat in case the am/pm marker preceeds the time specification. Locales with am/pm marker preceeding time spec by default exist (e. g. ko_KR). =20 Also, the code expects that tm_hour might be set to an invalid value because the %p specifier is used in conjunction with %H. But this usage is invalid in itself and now catched as error condition after commit 343a2a558153 ("Cygwin: strptime: make sure to fail on invalid input digits"). =20 Change the %H/%I/%p handling according to GLibC, i. e. =20 - fix tm_hour for pm only if the time value has been specified as 12 hour time %I, and =20 - perform the fixup only after the entire input has been scanned. This decouples the fixup from the %p position relativ to %I. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/libc/strptime.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/winsup/cygwin/libc/strptime.cc b/winsup/cygwin/libc/strptime.cc index 353b8008a1c9..dc6771fe5e23 100644 --- a/winsup/cygwin/libc/strptime.cc +++ b/winsup/cygwin/libc/strptime.cc @@ -335,6 +335,8 @@ __strptime(const char *buf, const char *fmt, struct tm = *tm, const char *new_fmt; uint ulim; int ymd =3D 0; + bool got_I =3D false; + bool got_pm =3D false; =20 bp =3D (const unsigned char *)buf; const struct lc_time_T *_CurrentTimeLocale =3D __get_time_locale (locale); @@ -537,6 +539,7 @@ literal: case 'H': LEGAL_ALT(ALT_O); bp =3D conv_num(bp, &tm->tm_hour, 0, 23, ALT_DIGITS); + got_I =3D false; continue; =20 case 'l': /* The hour (12-hour clock representation). */ @@ -547,6 +550,7 @@ literal: bp =3D conv_num(bp, &tm->tm_hour, 1, 12, ALT_DIGITS); if (tm->tm_hour =3D=3D 12) tm->tm_hour =3D 0; + got_I =3D true; continue; =20 case 'j': /* The day of year. */ @@ -573,9 +577,7 @@ literal: case 'p': /* The locale's equivalent of AM/PM. */ bp =3D find_string(bp, &i, _ctloc(am_pm), NULL, 2, locale); - if (tm->tm_hour > 11) - return NULL; - tm->tm_hour +=3D i * 12; + got_pm =3D (i =3D=3D 1); LEGAL_ALT(0); continue; =20 @@ -751,8 +753,12 @@ literal: default: /* Unknown/unsupported conversion. */ return NULL; } + } =20 + if (got_I && got_pm) + tm->tm_hour +=3D 12; + if (bp && (era || got_eoff)) { /* Default to current era. */