Skip to content

Replace non-reentrant localtime with localtime_r in databaseConnection.cpp#13

Merged
mccaffers merged 2 commits intomainfrom
copilot/replace-localtime-with-localtime-r
Apr 6, 2026
Merged

Replace non-reentrant localtime with localtime_r in databaseConnection.cpp#13
mccaffers merged 2 commits intomainfrom
copilot/replace-localtime-with-localtime-r

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 6, 2026

std::localtime uses a shared internal buffer, making it unsafe in multi-threaded contexts. Replaced with the reentrant localtime_r, which writes into a caller-supplied struct tm.

Changes

  • source/databaseConnection.cpp
    • Swap std::localtimelocaltime_r with a stack-allocated, zero-initialized struct tm
    • Guard against localtime_r returning nullptr (invalid time conversion), logging an error and skipping the entry
// Before
auto tm = *std::localtime(&time_t);

// After
struct tm tm = {};
if (localtime_r(&time_t, &tm) == nullptr) {
    std::cerr << "Error: failed to convert timestamp" << std::endl;
    continue;
}

Copilot AI changed the title [WIP] Replace non reentrant function localtime with localtime_r Replace non-reentrant localtime with localtime_r in databaseConnection.cpp Apr 6, 2026
Copilot AI requested a review from mccaffers April 6, 2026 17:46
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 6, 2026

@mccaffers mccaffers marked this pull request as ready for review April 6, 2026 17:56
Copilot AI review requested due to automatic review settings April 6, 2026 17:56
@mccaffers mccaffers merged commit 934e8d3 into main Apr 6, 2026
3 checks passed
@mccaffers mccaffers deleted the copilot/replace-localtime-with-localtime-r branch April 6, 2026 17:59
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::localtime usage with localtime_r and a caller-provided tm buffer.
  • 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.

Comment on lines +64 to +66
struct tm tm = {};
if (localtime_r(&time_t, &tm) == nullptr) {
std::cerr << "Error: failed to convert timestamp" << std::endl;
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +65 to +68
if (localtime_r(&time_t, &tm) == nullptr) {
std::cerr << "Error: failed to convert timestamp" << std::endl;
continue;
}
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace this call to the non reentrant function "localtime" by a call to "localtime_r".

3 participants