: std::put_time should not crash on invalid/out-of-range tm struct values · Issue #4882 · microsoft/STL (original) (raw)
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(¤tTime, &t);
currentTime.tm_hour = 25; // set invalid hour
stringstream ss;
ss << put_time(¤tTime, "%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.