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

StringMaker<std::chrono::time_point<std::chrono::system_clock, Duration>> crashes with dates before 1970 on Windows #2944

Open
sketchylizard opened this issue Dec 31, 2024 · 1 comment
Labels

Comments

@sketchylizard
Copy link
Contributor

Describe the bug
When trying to stringify a chrono::time_point, the Windows code calls gmtime_s which fails for dates before 1970. The return value (22) is not checked and the std::tm struct is left with -1 values for everything. When calling strftime it asserts because the month value is out of range (0..11). In release mode it crashes.

Expected behavior
At the very least, it should not crash. Ideally, it should handle dates older than 1970 (I assume any date with a negative duration.) Libc++ and libstdc++ both seem to handle these dates fine.

Reproduction steps
Steps to reproduce the bug.

#include <chrono>
#include <iostream>

#include "catch_amalgamated.hpp"

using namespace std::chrono;
using namespace std::literals::chrono_literals;

TEST_CASE("TestOldDates")
{
  static constexpr system_clock::time_point c_Dec30_1899 = sys_days(December / 30 / 1899);

  Catch::StringMaker<system_clock::time_point> sm;
  // Next line will assert/crash
  auto s = sm.convert(c_Dec30_1899);
  std::cout << s << "\n";

  // Next line will crash when it tries to output the values.
  CHECK(c_Dec30_1899 != c_Dec30_1899);
}

Platform information:

  • OS: Windows NT
  • Compiler+version: Microsoft Visual Studio Professional 2022, Version 17.11.6
  • Catch version: v3.7.1

Additional context
Microsoft's implementation of gmtime_s, along with std::gmtime all return an error if the input time is negative. Maybe this is really a Microsoft bug?

@horenmar
Copy link
Member

horenmar commented Jan 3, 2025

I see no good reason for the MS's libc behaviour here, but I agree that the result should not be a crash.

@horenmar horenmar added the Bug label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants