public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "webrown.cpp at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/95992] New: chrono adding duration to time_point reports signed integer overflow with -fsanitize=undefined
Date: Tue, 30 Jun 2020 20:25:00 +0000	[thread overview]
Message-ID: <bug-95992-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2020-06-30 20:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-30 20:25 webrown.cpp at gmail dot com [this message]
2020-06-30 23:18 ` [Bug libstdc++/95992] " redi at gcc dot gnu.org
2020-07-01 13:14 ` redboltz at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95992-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).