libstdc++: Fix arithmetic bug in year_month_weekday conversion [PR96713]
The conversion function year_month_weekday::operator sys_days computes the offset in days from the first weekday of the month with: days{(index()-1)*7} ^~~~~~~~~~~~~ type 'unsigned' We want the above to yield -7d when index() is 0u, but our 'days' alias is based on long instead of int, so the conversion from unsigned to the underlying type of 'days' instead yields a large positive value. This patch fixes this by casting the result of index() to int so that the initializer is sign-extended in the conversion to long. The added testcase also verifies we do the right thing when index() == 5. libstdc++-v3/ChangeLog: PR libstdc++/96713 * include/std/chrono (year_month_weekday::operator sys_days): Cast the result of index() to int so that the initializer for days{} is sign-extended when it's converted to the underlying type. * testsuite/std/time/year_month_weekday/3.cc: New test.
This commit is contained in:
parent
f4af2dde57
commit
8572edc828
2 changed files with 67 additions and 1 deletions
|
@ -2719,7 +2719,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|||
operator sys_days() const noexcept
|
||||
{
|
||||
auto __d = sys_days{year() / month() / 1};
|
||||
return __d + (weekday() - chrono::weekday(__d) + days{(index()-1)*7});
|
||||
return __d + (weekday() - chrono::weekday(__d)
|
||||
+ days{(static_cast<int>(index())-1)*7});
|
||||
}
|
||||
|
||||
explicit constexpr
|
||||
|
|
65
libstdc++-v3/testsuite/std/time/year_month_weekday/3.cc
Normal file
65
libstdc++-v3/testsuite/std/time/year_month_weekday/3.cc
Normal file
|
@ -0,0 +1,65 @@
|
|||
// { dg-options "-std=gnu++2a" }
|
||||
// { dg-do compile { target c++2a } }
|
||||
|
||||
// Copyright (C) 2020 Free Software Foundation, Inc.
|
||||
//
|
||||
// This file is part of the GNU ISO C++ Library. This library is free
|
||||
// software; you can redistribute it and/or modify it under the
|
||||
// terms of the GNU General Public License as published by the
|
||||
// Free Software Foundation; either version 3, or (at your option)
|
||||
// any later version.
|
||||
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this library; see the file COPYING3. If not see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
// PR libstdc++/97613
|
||||
// Test year_month_weekday to sys_days conversion for extreme values of index().
|
||||
|
||||
#include <chrono>
|
||||
|
||||
void
|
||||
test01()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
using ymd = year_month_day;
|
||||
|
||||
static_assert(ymd{sys_days{2020y/January/Sunday[0]}} == 2019y/December/29);
|
||||
static_assert(ymd{sys_days{2020y/January/Monday[0]}} == 2019y/December/30);
|
||||
static_assert(ymd{sys_days{2020y/January/Tuesday[0]}} == 2019y/December/31);
|
||||
static_assert(ymd{sys_days{2020y/January/Wednesday[0]}} == 2019y/December/25);
|
||||
static_assert(ymd{sys_days{2020y/January/Thursday[0]}} == 2019y/December/26);
|
||||
static_assert(ymd{sys_days{2020y/January/Friday[0]}} == 2019y/December/27);
|
||||
static_assert(ymd{sys_days{2020y/January/Saturday[0]}} == 2019y/December/28);
|
||||
|
||||
static_assert((2020y).is_leap());
|
||||
static_assert(ymd{sys_days{2020y/March/Sunday[0]}} == 2020y/February/23);
|
||||
static_assert(ymd{sys_days{2020y/March/Monday[0]}} == 2020y/February/24);
|
||||
static_assert(ymd{sys_days{2020y/March/Tuesday[0]}} == 2020y/February/25);
|
||||
static_assert(ymd{sys_days{2020y/March/Wednesday[0]}} == 2020y/February/26);
|
||||
static_assert(ymd{sys_days{2020y/March/Thursday[0]}} == 2020y/February/27);
|
||||
static_assert(ymd{sys_days{2020y/March/Friday[0]}} == 2020y/February/28);
|
||||
static_assert(ymd{sys_days{2020y/March/Saturday[0]}} == 2020y/February/29);
|
||||
|
||||
static_assert(!(2019y).is_leap());
|
||||
static_assert(ymd{sys_days{2019y/March/Sunday[0]}} == 2019y/February/24);
|
||||
static_assert(ymd{sys_days{2019y/March/Monday[0]}} == 2019y/February/25);
|
||||
static_assert(ymd{sys_days{2019y/March/Tuesday[0]}} == 2019y/February/26);
|
||||
static_assert(ymd{sys_days{2019y/March/Wednesday[0]}} == 2019y/February/27);
|
||||
static_assert(ymd{sys_days{2019y/March/Thursday[0]}} == 2019y/February/28);
|
||||
static_assert(ymd{sys_days{2019y/March/Friday[0]}} == 2019y/February/22);
|
||||
static_assert(ymd{sys_days{2019y/March/Saturday[0]}} == 2019y/February/23);
|
||||
|
||||
static_assert(ymd{sys_days{2020y/December/Sunday[5]}} == 2021y/January/3);
|
||||
static_assert(ymd{sys_days{2020y/December/Monday[5]}} == 2021y/January/4);
|
||||
static_assert(ymd{sys_days{2020y/December/Tuesday[5]}} == 2020y/December/29);
|
||||
static_assert(ymd{sys_days{2020y/December/Wednesday[5]}} == 2020y/December/30);
|
||||
static_assert(ymd{sys_days{2020y/December/Thursday[5]}} == 2020y/December/31);
|
||||
static_assert(ymd{sys_days{2020y/December/Friday[5]}} == 2021y/January/1);
|
||||
static_assert(ymd{sys_days{2020y/December/Saturday[5]}} == 2021y/January/2);
|
||||
}
|
Loading…
Add table
Reference in a new issue