Skip to content

fix: zero-init struct tm before gmtime_r; replace strcpy with memcpy#2753

Open
stark256-spec wants to merge 1 commit into
nasa:devfrom
stark256-spec:fix/time-gmtime-uninitialized-and-strcpy
Open

fix: zero-init struct tm before gmtime_r; replace strcpy with memcpy#2753
stark256-spec wants to merge 1 commit into
nasa:devfrom
stark256-spec:fix/time-gmtime-uninitialized-and-strcpy

Conversation

@stark256-spec
Copy link
Copy Markdown

Fixes #2735 and #2737.

#2735gmtime_r() returns NULL for out-of-range time_t values, leaving struct tm uninitialised. strftime() on uninitialised memory is UB. Zero-initialise tm with memset() before calling gmtime_r() so a failed conversion produces a deterministic epoch-like string.

#2737 — Both strcpy sites in cfe_assert_init.c and cfe_tbl_internal.c append a fixed-length literal into a buffer where space was already verified. Replace with memcpy(dst, literal, sizeof(literal)) to copy exactly the required bytes including the NUL terminator.

Two independent static-analysis findings addressed in one commit since
they touch disjoint files and carry the same risk category.

1. cfe_time_api.c — CFE_TIME_Print (fixes nasa#2735)
   gmtime_r() returns NULL when the input time_t is outside the range
   representable by struct tm (overflow or platform-specific limits).
   The subsequent strftime() then reads from an uninitialised struct,
   producing undefined behaviour. Zero-initialise tm with memset before
   calling gmtime_r() so that a failed conversion yields a deterministic
   epoch-like formatted string rather than garbage or a crash.

2. cfe_assert_init.c + cfe_tbl_internal.c — strcpy (fixes nasa#2737)
   Both sites append a known, fixed-length literal ('.tmp' and '(*)')
   into a buffer where available space has already been verified by the
   surrounding bounds check. Replace strcpy with memcpy(dst, literal,
   sizeof(literal)) which copies exactly the required bytes including the
   NUL terminator without relying on runtime null-termination scanning.
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.

Uninitialized Variable in cfe_time_api.c

2 participants