]> gcc.gnu.org Git - gcc.git/commit
libstdc++: Fix UB in weekday::weekday(sys_days) and add test
authorCassio Neri <cassio.neri@gmail.com>
Sun, 12 Nov 2023 01:33:52 +0000 (01:33 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 14 Nov 2023 22:32:08 +0000 (22:32 +0000)
commitf6ce081d0ffb5f25d71eb2f30fcfdff7f20dba22
treebeb9a50210c8d8edcae4a919c9dc678cfac1cc23
parent86a0df1a6c7fe4a835620b868e76ea78d42d6620
libstdc++: Fix UB in weekday::weekday(sys_days) and add test

The following has undefined behaviour (signed overflow) [1]:
    weekday max{sys_days{days{numeric_limits<days::rep>::max()}}};

The issue is in this line when __n is very large and __n + 4 overflows:
    return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6);

In addition to fixing this bug, the new implementation makes the compiler emit
shorter and branchless code for x86-64 and ARM [2].

[1] https://godbolt.org/z/1s5bv7KfT
[2] https://godbolt.org/z/zKsabzrhs

libstdc++-v3/ChangeLog:

* include/std/chrono (weekday::_S_from_days): Fix UB.
* testsuite/std/time/weekday/1.cc: Add test for overflow.
libstdc++-v3/include/std/chrono
libstdc++-v3/testsuite/std/time/weekday/1.cc
This page took 0.060126 seconds and 5 git commands to generate.