Skip to content

Commit

Permalink
fix: replace asctime with strftime for safer date formatting (#3658)
Browse files Browse the repository at this point in the history
Replaced the use of the obsolete `asctime` with `strftime` to ensure thread safety and provide better control over date formatting. `asctime` uses a static internal buffer, which can lead to issues in multi-threaded environments, while `strftime` allows for custom formats and avoids these problems.
  • Loading branch information
AJPfleger authored Sep 29, 2024
1 parent 6a8c643 commit c93ff9f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Plugins/Json/src/DetectorJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <map>
#include <memory>
#include <set>
#include <string>

nlohmann::json Acts::DetectorJsonConverter::toJson(
const GeometryContext& gctx, const Experimental::Detector& detector,
Expand Down Expand Up @@ -81,7 +82,9 @@ nlohmann::json Acts::DetectorJsonConverter::toJson(
nlohmann::json jHeader;
jHeader["detector"] = detector.name();
jHeader["type"] = "acts";
jHeader["date"] = std::asctime(ti);
char buffer[100];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ti);
jHeader["date"] = std::string(buffer);
jHeader["surface_count"] = nSurfaces;
jHeader["portal_count"] = portals.size();
jHeader["volume_count"] = detector.volumes().size();
Expand All @@ -102,7 +105,9 @@ nlohmann::json Acts::DetectorJsonConverter::toJsonDetray(

nlohmann::json jCommonHeader;
jCommonHeader["detector"] = detector.name();
jCommonHeader["date"] = std::asctime(ti);
char buffer[100];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ti);
jCommonHeader["date"] = std::string(buffer);
jCommonHeader["version"] = "detray - 0.44.0";
jCommonHeader["tag"] = "geometry";

Expand Down

0 comments on commit c93ff9f

Please sign in to comment.