Skip to content

<iomanip>: std::put_time should not crash on invalid/out-of-range tm struct values #4882

Description

Describe the bug

If an invalid or out-of-range tm struct is passed to std::put_time() it will lead to crash.

Command-line test case

C:\Temp>type repro.cpp
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctime>

int main() {
    using namespace std;

    time_t t = time(nullptr);
    tm currentTime;
    localtime_s(&currentTime, &t);
    currentTime.tm_hour = 25; // set invalid hour

    stringstream ss;
    ss << put_time(&currentTime, "%Y-%m-%d-%H-%M");
}

C:\Temp>cl /EHsc /W4 /WX /std:c++20 .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32546 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 19.36.32546.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
!!!App Crashed!!!

Expected behavior

The program should NOT crash.

Additional context

The crash is happening because of strftime which validates the data and if something is wrong it leads to crash if no handler is set.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions