Skip to content

<chrono>/<format>: Using milliseconds::max() triggers icx' UBSan #6277

@bersbersbers

Description

@bersbersbers

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    LWG issue neededA wording defect that should be submitted to LWG as a new issuechronoC++20 chronoformatC++20/23 format

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions