public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0
@ 2020-10-28 8:52 faithandbrave at gmail dot com
2020-10-28 9:05 ` [Bug libstdc++/97613] " faithandbrave at gmail dot com
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: faithandbrave at gmail dot com @ 2020-10-28 8:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97613
Bug ID: 97613
Summary: chrono::year_month_weekday cast to sys_days : return
bad value if index() == 0
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: faithandbrave at gmail dot com
Target Milestone: ---
#include <iostream>
#include <chrono>
namespace chrono = std::chrono;
using namespace std::chrono_literals;
int main()
{
// 1
chrono::sys_days date1 = 2020y/3/chrono::Sunday[1];
assert(chrono::year_month_day{date1} == 2020y/3/1);
// 2
chrono::sys_days date2 = 2020y/3/chrono::Sunday[2];
assert(chrono::year_month_day{date2} == 2020y/3/8);
// 3
chrono::sys_days date3 = 2020y/3/chrono::Sunday[0];
assert(chrono::year_month_day{date3} == 2020y/2/23);
}
Assertion failed the program's No.3 patten.
GCC 11 (trunk) returns `30297y/March/14d`. Clang (libc++) is OK.
See the specification:
> 27.8.16.2 [time.cal.ymwd.members]
> If index() is 0 the returned sys_days represents the date 7 days prior to the first weekday() of year()/month().
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/97613] chrono::year_month_weekday cast to sys_days : return bad value if index() == 0
2020-10-28 8:52 [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0 faithandbrave at gmail dot com
@ 2020-10-28 9:05 ` faithandbrave at gmail dot com
2020-10-28 13:45 ` ppalka at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: faithandbrave at gmail dot com @ 2020-10-28 9:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97613
--- Comment #1 from Akira Takahashi <faithandbrave at gmail dot com> ---
- #include <iostream>
+ #include <cassert>
#include <cassert>
#include <chrono>
namespace chrono = std::chrono;
using namespace std::chrono_literals;
int main()
{
chrono::sys_days date1 = 2020y/3/chrono::Sunday[1];
assert(chrono::year_month_day{date1} == 2020y/3/1);
chrono::sys_days date2 = 2020y/3/chrono::Sunday[2];
assert(chrono::year_month_day{date2} == 2020y/3/8);
chrono::sys_days date3 = 2020y/3/chrono::Sunday[0];
assert(chrono::year_month_day{date3} == 2020y/2/23);
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/97613] chrono::year_month_weekday cast to sys_days : return bad value if index() == 0
2020-10-28 8:52 [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0 faithandbrave at gmail dot com
2020-10-28 9:05 ` [Bug libstdc++/97613] " faithandbrave at gmail dot com
@ 2020-10-28 13:45 ` ppalka at gcc dot gnu.org
2020-10-28 16:41 ` ppalka at gcc dot gnu.org
2020-10-28 16:44 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2020-10-28 13:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97613
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Status|UNCONFIRMED |ASSIGNED
Assignee|unassigned at gcc dot gnu.org |ppalka at gcc dot gnu.org
Last reconfirmed| |2020-10-28
--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Confirmed.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/97613] chrono::year_month_weekday cast to sys_days : return bad value if index() == 0
2020-10-28 8:52 [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0 faithandbrave at gmail dot com
2020-10-28 9:05 ` [Bug libstdc++/97613] " faithandbrave at gmail dot com
2020-10-28 13:45 ` ppalka at gcc dot gnu.org
@ 2020-10-28 16:41 ` ppalka at gcc dot gnu.org
2020-10-28 16:44 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2020-10-28 16:41 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97613
--- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:
https://gcc.gnu.org/g:8572edc828f6d1e7c8243f901fe7c96f62a11a8e
commit r11-4490-g8572edc828f6d1e7c8243f901fe7c96f62a11a8e
Author: Patrick Palka <ppalka@redhat.com>
Date: Wed Oct 28 12:28:08 2020 -0400
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/97613] chrono::year_month_weekday cast to sys_days : return bad value if index() == 0
2020-10-28 8:52 [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0 faithandbrave at gmail dot com
` (2 preceding siblings ...)
2020-10-28 16:41 ` ppalka at gcc dot gnu.org
@ 2020-10-28 16:44 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2020-10-28 16:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97613
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
Target Milestone|--- |11.0
--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
This should be fixed now, thanks for the report!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-10-28 16:44 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-28 8:52 [Bug libstdc++/97613] New: chrono::year_month_weekday cast to sys_days : return bad value if index() == 0 faithandbrave at gmail dot com
2020-10-28 9:05 ` [Bug libstdc++/97613] " faithandbrave at gmail dot com
2020-10-28 13:45 ` ppalka at gcc dot gnu.org
2020-10-28 16:41 ` ppalka at gcc dot gnu.org
2020-10-28 16:44 ` ppalka at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).