Skip to content

Commit

Permalink
libc/minimal: fix reproducibility of gmtime
Browse files Browse the repository at this point in the history
struct tm has fields that were not being set by the implementation,
causing the test to fail when the uninitialized values were compared
with a static initialized result.  Zero the structure before filling it.

Closes #17794

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
  • Loading branch information
pabigot authored and ioannisg committed Jul 31, 2019
1 parent 25bca3c commit b8af1a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/libc/minimal/source/time/gmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ struct tm *gmtime_r(const time_t *_MLIBC_RESTRICT timep,
bigint_type days = (z >= 0 ? z : z - 86399) / 86400;
unsigned int rem = z - days * 86400;

*tp = (struct tm){ 0 };

time_civil_from_days(days, tp);

tp->tm_hour = rem / 60U / 60U;
Expand Down
5 changes: 4 additions & 1 deletion tests/lib/timeutil/src/test_gmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

void test_gmtime(void)
{
struct tm tm;
struct tm tm = {
/* Initialize an unset field */
.tm_isdst = 1234,
};
time_t time = 1561994005;

zassert_equal(&tm, gmtime_r(&time, &tm),
Expand Down

0 comments on commit b8af1a6

Please sign in to comment.