Skip to content

clang optimization ignores UB (overflow) + standard library bug #130229

Open
@kelbon

Description

@kelbon

https://godbolt.org/z/r8Yd7nhzc

Not sure standard requires this behavior or not, but it is undefined behavior in IMPLICI T conversion, in naive code, which is expected to be working. Huge disappointment in std::chrono

#include <chrono>
#include <iostream>
int main() {
    std::chrono::nanoseconds ns = std::chrono::milliseconds::max();
    std::cout << ns.count() << "|" << ns.max().count();
}

Output:
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/chrono.h:225:38: runtime error: signed integer overflow: 9223372036854775807 * 1000000 cannot be represented in type 'long int'
-1000000|9223372036854775807

Expected: max count of nanoseconds

Additionaly: clang (when writed as one line) ignores undefined behavior while constant folding

Metadata

Metadata

Assignees

No one assigned

    Labels

    chronoIssues related to std::chronoclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"compiler-rt:ubsanUndefined behavior sanitizerlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.undefined behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions