Skip to content

lib/datetime: Fix reliability issues and replace non-reentrant time functions#7597

Draft
NeelGhoshal wants to merge 4 commits into
OSGeo:mainfrom
NeelGhoshal:fix/lib-datetime-reliability-reentrant
Draft

lib/datetime: Fix reliability issues and replace non-reentrant time functions#7597
NeelGhoshal wants to merge 4 commits into
OSGeo:mainfrom
NeelGhoshal:fix/lib-datetime-reliability-reentrant

Conversation

@NeelGhoshal

Copy link
Copy Markdown
Contributor

Description

Fix a reliability bug and replace two non-reentrant time functions in
lib/datetime.

File Category Fix
same.c Reliability Compare DateTime structs member-by-member instead of with memcmp; the struct contains padding bytes whose values are unspecified, making memcmp incorrect for equality checks
local.c Maintainability Replace non-reentrant localtime() with localtime_r() (×2)
local.c Maintainability Replace non-reentrant gmtime() with gmtime_r()

Motivation and context

The memcmp equality check on a struct with padding is undefined behaviour —
padding bytes are not guaranteed to be zeroed, so two logically equal
DateTime values can compare as unequal. Member-by-member comparison
avoids this.

localtime() and gmtime() return pointers to a shared static buffer,
making them unsafe in multithreaded code. The _r variants write to a
caller-supplied buffer.

How has this been tested?

lib/datetime has no dedicated C unit test suite. The functions are
exercised indirectly through the temporal module and related tools.
These changes are behaviour-preserving for single-threaded use and
correctness-fixing for the struct comparison.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • PR title provides summary of the changes and starts with one of the pre-defined prefixes
  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have added tests to cover my changes

Comment thread lib/datetime/same.c
Comment on lines +24 to +32
return src->year == dst->year &&
src->month == dst->month &&
src->day == dst->day &&
src->hour == dst->hour &&
src->minute == dst->minute &&
src->second == dst->second &&
src->usec == dst->usec &&
src->timezone == dst->timezone;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[pre-commit] reported by reviewdog 🐶

Suggested change
return src->year == dst->year &&
src->month == dst->month &&
src->day == dst->day &&
src->hour == dst->hour &&
src->minute == dst->minute &&
src->second == dst->second &&
src->usec == dst->usec &&
src->timezone == dst->timezone;
return src->year == dst->year && src->month == dst->month &&
src->day == dst->day && src->hour == dst->hour &&
src->minute == dst->minute && src->second == dst->second &&
src->usec == dst->usec && src->timezone == dst->timezone;

@echoix

echoix commented Jun 23, 2026

Copy link
Copy Markdown
Member

Since you’re using an AI assistance, could you try harder to add some "unit" tests for this, and after that also your other recent PRs?

Ideally in pytest (since we have the architecture for it), if the functions are public, so available in the ctypes bindings. You can’t really assume that the pre-existing state is tested well enough

@github-actions github-actions Bot added C Related code is in C libraries labels Jun 23, 2026
@NeelGhoshal NeelGhoshal marked this pull request as draft June 23, 2026 20:47
@nilason

nilason commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

You obviously didn’t test locally, read CONTRIBUTING.md, and do not waste others time. (Pun intended).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants