https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95992
Bug ID: 95992 Summary: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: redboltz at gmail dot com CC: webrown.cpp at gmail dot com Target Milestone: --- CC: webrown.cpp at gmail dot com When I add a duration to std::chrono::time_point<std::chrono::system_clock>, then signed integer overflow is reported on runtime. The compile option I set is -fsanitize=undefined -std=gnu++2a I call test() three times but the error is reported only the first call. I think that it is weird. Code: #include <chrono> #include <iostream> using duration_t = std::chrono::system_clock::duration; void test() { std::chrono::time_point<std::chrono::system_clock> tp; auto ns = std::chrono::nanoseconds(145224192L); auto s = std::chrono::seconds(-9223372037LL); tp += std::chrono::duration_cast<duration_t>(ns); std::cout << "before add sec" << std::endl; tp += std::chrono::duration_cast<duration_t>(s); std::cout << "after add sec" << std::endl; } int main() { std::cout << "<<< 1 >>>" << std::endl; test(); std::cout << "<<< 2 >>>" << std::endl; test(); std::cout << "<<< 3 >>>" << std::endl; test(); } Output: <<< 1 >>> before add sec /opt/wandbox/gcc-10.1.0/include/c++/10.1.0/chrono:197:38: runtime error: signed integer overflow: -9223372037 * 1000000000 cannot be represented in type 'long int' /opt/wandbox/gcc-10.1.0/include/c++/10.1.0/chrono:474:8: runtime error: signed integer overflow: 9223372036709551616 + 145224192 cannot be represented in type 'long int' after add sec <<< 2 >>> before add sec after add sec <<< 3 >>> before add sec after add sec Runable Demo: g++ 10.1.0 https://wandbox.org/permlink/uQja7r4XZYyGIMjI clang++ 10.0.0 (libc++) https://wandbox.org/permlink/aDskiG7oaKj2R2dW Error reported case: * g++ 10.1.0 with libstdc++ 10.1.0 * clang++ 10.0.0 with libstdc++ 10.1.0 No error case: * clang++ 10.0.0 with libc++ 10.0.0 Environment: Linux 5.7.4-arch1-1 #1 SMP PREEMPT Thu, 18 Jun 2020 16:01:07 +0000 x86_64 GNU/Linux