`<chrono>`/`<format>`: Using `milliseconds::max()` triggers icx' UBSan
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.1k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
Describe the bug
Formatting std::chrono::milliseconds::max() using std::format triggers icx' UBSan. Output below.
There is an integer overflow in converting std::chrono::milliseconds::max() to std::chrono::hh_mm_ss.
Note: I did not expect std::format("{:%Q}", ...) to even trigger conversion to std::chrono::hh_mm_ss.
Command-line test case
C:\Temp>type repro.cpp
#include <chrono>
#include <format>
int main() {
auto duration = std::chrono::milliseconds::max();
auto unit = std::format("{:%Q}", duration);
std::chrono::hh_mm_ss time(duration);
}
C:\Temp>"C:\Program Files (x86)\Intel\oneAPI\compiler\2024.2\bin\icx" repro.cpp -o repro.exe -std:c++20 -D_MSVC_STL_DOOM_FUNCTION(x)=__debugbreak() -fsanitize=undefined /link /NODEFAULTLIB:libmmt.lib /LIBPATH:"C:\Program Files (x86)\Intel\oneAPI\compiler\2024.2\lib"
Intel(R) oneAPI DPC++/C++ Compiler for applications running on Intel(R) 64, Version 2024.2.0 Build 20240602
Copyright (C) 1985-2024 Intel Corporation. All rights reserved.
Creating library repro.lib and object repro.exp
C:\Temp>.\repro.exe
C:\Program Files\Microsoft Visual Studio\18\Professional\VC\Tools\MSVC\14.50.35717\include\__msvc_chrono.hpp:300:39: runtime error: signed integer overflow: 9223372036854775807 - -7371675709200000 cannot be represented in type 'long long'
#0 0x7ff7839212bd (C:\Temp\repro.exe+0x1400012bd)
#1 0x7ff7839420e3 (C:\Temp\repro.exe+0x1400220e3)
#2 0x7ff78392301a (C:\Temp\repro.exe+0x14000301a)
#3 0x7ff783922753 (C:\Temp\repro.exe+0x140002753)
#4 0x7ff783921b80 (C:\Temp\repro.exe+0x140001b80)
#5 0x7ff78392149d (C:\Temp\repro.exe+0x14000149d)
#6 0x7ff783921092 (C:\Temp\repro.exe+0x140001092)
#7 0x7ff7839795eb in __ubsan_handle_cfi_bad_type (C:\Temp\repro.exe+0x1400595eb)
#8 0x7ffc9ac8e8d6 (C:\WINDOWS\System32\KERNEL32.DLL+0x18002e8d6)
#9 0x7ffc9ce8c3fb (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18008c3fb)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files\Microsoft Visual Studio\18\Professional\VC\Tools\MSVC\14.50.35717\include\__msvc_chrono.hpp:300:39
Expected behavior
UBSan not triggering
STL version
202508 (14.50.35717)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the repro.cpp case using std::format("{:%Q}", duration) and std::chrono::hh_mm_ss, then inspect the reported __msvc_chrono.hpp line 300 and the corresponding STL source. Run the supplied icx UBSan command; done means formatting std::chrono::milliseconds::max() no longer triggers signed-overflow UBSan.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100