From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D0E283857BB3; Fri, 1 Sep 2023 10:55:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0E283857BB3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693565704; bh=1TInz7SbnC3mKMPC2Xu0XiYr7R1/KtvK8KrINYRgtCg=; h=From:To:Subject:Date:From; b=MTOvbX5YvwB2bQwDBVQ89W93SafA4ogCSQe5eO78zO83Cpse7xwq3PhT8vY0AyYKp RNxRBiQJYsXysbN408/O3Wkfh7DeanxgWbLkvAan11BlByhxLtoeMFQCA3EYHkpHZz /G1ccP14nsAdJklWO4l8QZhrPi0Qdx2nQqooUaFg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-3609] libstdc++: Do not allow chrono::parse to overflow for %C [PR111162] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 17a371d05a796ba8df0300afa4ddc3bdef523f9a X-Git-Newrev: 207c507499d23f0176cbfdfe96d3cd50dec39584 Message-Id: <20230901105504.D0E283857BB3@sourceware.org> Date: Fri, 1 Sep 2023 10:55:04 +0000 (GMT) List-Id: https://gcc.gnu.org/g:207c507499d23f0176cbfdfe96d3cd50dec39584 commit r14-3609-g207c507499d23f0176cbfdfe96d3cd50dec39584 Author: Jonathan Wakely Date: Tue Aug 29 13:07:21 2023 +0100 libstdc++: Do not allow chrono::parse to overflow for %C [PR111162] libstdc++-v3/ChangeLog: PR libstdc++/111162 * include/bits/chrono_io.h (_Parser::Operator()): Check %C values are in range of year::min() to year::max(). * testsuite/std/time/parse.cc: Check out of range centuries. Diff: --- libstdc++-v3/include/bits/chrono_io.h | 9 ++++++++- libstdc++-v3/testsuite/std/time/parse.cc | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/chrono_io.h b/libstdc++-v3/include/bits/chrono_io.h index d558802e7d86..f359571b4dba 100644 --- a/libstdc++-v3/include/bits/chrono_io.h +++ b/libstdc++-v3/include/bits/chrono_io.h @@ -3171,7 +3171,14 @@ namespace __detail { auto __v = __read_signed(__num ? __num : 2); if (!__is_failed(__err)) - __century = __v * 100; + { + int __cmin = (int)year::min() / 100; + int __cmax = (int)year::max() / 100; + if (__cmin <= __v && __v <= __cmax) + __century = __v * 100; + else + __century = -2; // This prevents guessing century. + } } else if (__mod == 'E') { diff --git a/libstdc++-v3/testsuite/std/time/parse.cc b/libstdc++-v3/testsuite/std/time/parse.cc index 9b36c5d7db4c..46eb7f28c85e 100644 --- a/libstdc++-v3/testsuite/std/time/parse.cc +++ b/libstdc++-v3/testsuite/std/time/parse.cc @@ -251,6 +251,18 @@ test_errors() is >> parse("%H:%M %3y", y); // 61min is out of range but not needed VERIFY( is.eof() && ! is.fail() ); VERIFY( y == 2010y ); + + is.clear(); + is.str("328 00"); + is >> parse("%3C %y", y); // 328 is out of range for %C (PR libstdc++/111162) + VERIFY( is.fail() ); + VERIFY( y == 2010y ); + + is.clear(); + is.str("-328 00"); + is >> parse("%3C %y", y); // -328 is out of range for %C + VERIFY( is.fail() ); + VERIFY( y == 2010y ); } void