Conversation
…, check return value Agent-Logs-Url: https://github.com/mccaffers/backtesting-engine-cpp/sessions/b4ab18b5-0938-4228-8b14-46cee2a9f7e1 Co-authored-by: mccaffers <16667079+mccaffers@users.noreply.github.com>
localtime with localtime_r in databaseConnection.cpp
|
There was a problem hiding this comment.
Pull request overview
This PR updates DatabaseConnection::printResults to avoid the non-reentrant std::localtime by switching to a reentrant local-time conversion function and handling conversion failures.
Changes:
- Replace
std::localtimeusage withlocaltime_rand a caller-providedtmbuffer. - Add a failure guard for timestamp conversion that logs an error and skips the affected entry.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| struct tm tm = {}; | ||
| if (localtime_r(&time_t, &tm) == nullptr) { | ||
| std::cerr << "Error: failed to convert timestamp" << std::endl; |
There was a problem hiding this comment.
localtime_r is a POSIX API and is not available under MSVC/Windows builds. Since the project’s CMakeLists explicitly supports MSVC, this change can break Windows compilation. Consider adding a small platform abstraction (e.g., #if defined(_WIN32) use localtime_s(&tm, &time_t); otherwise localtime_r) and qualifying the call (::localtime_r) with the appropriate <ctime>/<time.h> include so the declaration is guaranteed to be visible.
| if (localtime_r(&time_t, &tm) == nullptr) { | ||
| std::cerr << "Error: failed to convert timestamp" << std::endl; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
This change introduces a new failure branch (localtime_r(...) == nullptr) that alters output behavior (skips entries). The repository has XCTest coverage for DatabaseConnection (e.g., tests/db.mm), but there’s no test exercising printResults timestamp formatting or the new error path. Consider extracting the timestamp-to-string formatting into a helper that can be unit tested (including the failure case) without relying on stdout/stderr capture.



std::localtimeuses a shared internal buffer, making it unsafe in multi-threaded contexts. Replaced with the reentrantlocaltime_r, which writes into a caller-suppliedstruct tm.Changes
source/databaseConnection.cppstd::localtime→localtime_rwith a stack-allocated, zero-initializedstruct tmlocaltime_rreturningnullptr(invalid time conversion), logging an error and skipping the entry