Skip to content

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

@sketchylizard

Description

@sketchylizard

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions