[Bug libstdc++/126524] New: chrono::round<nanoseconds>(nanoseconds::max()) triggers integer overflow

rs2740 at gmail dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126524

            Bug ID: 126524
           Summary: chrono::round<nanoseconds>(nanoseconds::max())
                    triggers integer overflow
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rs2740 at gmail dot com
  Target Milestone: ---

#include <chrono>
using namespace std::chrono;

int main() {
    (void) round<nanoseconds>(nanoseconds::max());
}

will error with -fsanitize=undefined:

/cefs/38/383ad2f84cbd57a52fd68bbe_consolidated/compilers_c++_x86_gcc_16.1.0/include/c++/16.1.0/bits/chrono.h:706:34:
runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be
represented in type 'long int'
/cefs/38/383ad2f84cbd57a52fd68bbe_consolidated/compilers_c++_x86_gcc_16.1.0/include/c++/16.1.0/bits/chrono.h:720:34:
runtime error: signed integer overflow: -9223372036854775808 -
9223372036854775807 cannot be represented in type 'long int'

I don't see anything in https://eel.is/c++draft/time.duration.cast#9 that bans
this.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.