Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++][format] Negation of LLONG_MIN (UB) when formatting chrono::duration #60123

Open
JMazurkiewicz opened this issue Jan 18, 2023 · 2 comments
Assignees
Labels
format C++20 std::format or std::print, and anything related to them libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@JMazurkiewicz
Copy link
Contributor

JMazurkiewicz commented Jan 18, 2023

Example:

#include <chrono>
#include <format>
#include <iostream>

using namespace std; 

int main() {
    cout << format("{:%Q}\n", chrono::nanoseconds::min());
}

Expected output:

-9223372036854775808

Got:

--9223372036854775808

Compiler explorer: https://godbolt.org/z/cvcYsTbM8
Similar issue in MSVC STL: microsoft/STL#1902

Comparison with fmtlib: https://godbolt.org/z/YEvPYWKTb

@JMazurkiewicz JMazurkiewicz changed the title [libc++][format] Negation of INT_MIN (UB) when formatting chrono::duration [libc++][format] Negation of LLONG_MIN (UB) when formatting chrono::duration Jan 18, 2023
@EugeneZelenko EugeneZelenko added libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. and removed new issue labels Jan 18, 2023
@mordante mordante self-assigned this Jan 18, 2023
@mordante
Copy link
Member

This seems like a bug in the Standard http://www.eelis.net/c++draft/time.format#4

The result of formatting a std​::​chrono​::​duration instance holding a negative value, or an hh_­mm_­ss object h for which h.is_­negative() is true, is equivalent to the output of the corresponding positive value, with a STATICALLY-WIDEN<charT>("-") character sequence placed before the replacement of the initial conversion specifier[.](http://www.eelis.net/c++draft/time.format#4.sentence-1)

There is no corresponding positive value for the minimum value. I'll investigate it later in more detail.

@jwakely
Copy link
Contributor

jwakely commented Apr 18, 2023

I think this needs an LWG issue.

FWIW libstdc++ prints the same thing, and also has a UBsan error.

@philnik777 philnik777 added the format C++20 std::format or std::print, and anything related to them label Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format C++20 std::format or std::print, and anything related to them libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

5 participants