public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/95992] New: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined
@ 2020-06-30 20:25 webrown.cpp at gmail dot com
2020-06-30 23:18 ` [Bug libstdc++/95992] " redi at gcc dot gnu.org
2020-07-01 13:14 ` redboltz at gmail dot com
0 siblings, 2 replies; 3+ messages in thread
From: webrown.cpp at gmail dot com @ 2020-06-30 20:25 UTC (permalink / raw)
To: gcc-bugs
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug libstdc++/95992] chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined
2020-06-30 20:25 [Bug libstdc++/95992] New: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined webrown.cpp at gmail dot com
@ 2020-06-30 23:18 ` redi at gcc dot gnu.org
2020-07-01 13:14 ` redboltz at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-30 23:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95992
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |INVALID
Status|UNCONFIRMED |RESOLVED
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I don't think there's any bug here. Libstdc++ uses nanoseconds for
chrono::system_clock::duration, but libc++ uses microseconds.
If you replace system_clock with high_resolution_clock you get the same error
with both libstdc++ and libc++, and the reason is that 9 billion seconds cannot
be represented in nanoseconds.
You need to use more suitable types for the durations you're trying to deal
with, e.g.
using duration_t = std::chrono::microseconds;
void test() {
std::chrono::time_point<std::chrono::system_clock, duration_t> tp;
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug libstdc++/95992] chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined
2020-06-30 20:25 [Bug libstdc++/95992] New: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined webrown.cpp at gmail dot com
2020-06-30 23:18 ` [Bug libstdc++/95992] " redi at gcc dot gnu.org
@ 2020-07-01 13:14 ` redboltz at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: redboltz at gmail dot com @ 2020-07-01 13:14 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95992
--- Comment #2 from Takatoshi Kondo <redboltz at gmail dot com> ---
Thank you.
I understood that I should use appropriate types.
I also understood why libc++ and libstdc++ behavior are different.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-07-01 13:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-30 20:25 [Bug libstdc++/95992] New: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined webrown.cpp at gmail dot com
2020-06-30 23:18 ` [Bug libstdc++/95992] " redi at gcc dot gnu.org
2020-07-01 13:14 ` redboltz at gmail dot com
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).