mirror of
https://gcc.gnu.org/git/gcc.git
synced 2024-12-05 09:44:10 +08:00
libstdc++: Add a test checking for chrono::duration overflows
This test fails if chrono::days::rep or chrono::years::rep is a 32-bit type, because a large days or years value silently overflows a 32-bit integer when converted to seconds. It would be conforming to implement chrono::days as chrono::duration<int32_t, ratio<86400>>, but would make this overflow case more likely. Similarly for chrono::years, chrono::months and chrono::weeks. This test is here to remind us not to make that change lightly. libstdc++-v3/ChangeLog: * testsuite/20_util/duration/arithmetic/overflow_c++20.cc: New test.
This commit is contained in:
parent
6c0f958401
commit
dc94eaab00
@ -0,0 +1,29 @@
|
||||
// { dg-options "-std=gnu++20" }
|
||||
// { dg-do run { target c++20 } }
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <testsuite_hooks.h>
|
||||
|
||||
void
|
||||
test_overflow()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
using seconds32_t = duration<std::int_least32_t>;
|
||||
seconds32_t t = 14h + 25min + 55s;
|
||||
auto snow = sys_days(1854y/December/11);
|
||||
auto snow_t = snow + t;
|
||||
// Fails if days::rep is 32-bit:
|
||||
VERIFY( snow_t.time_since_epoch() < seconds::zero() );
|
||||
auto y = floor<years>(snow);
|
||||
auto y_t = y + t;
|
||||
// Fails if years::rep is 32-bit:
|
||||
VERIFY( y_t.time_since_epoch() < seconds::zero() );
|
||||
VERIFY( y_t < snow_t );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_overflow();
|
||||
}
|
Loading…
Reference in New Issue
Block a user