Skip to content

Commit

Permalink
clock: Use 64-bit integer to measure time duration
Browse files Browse the repository at this point in the history
To ensure compatibility with platforms where long is a 32-bit
integer, the unsigned long oc_clock_seconds() function has been
replaced with uint64_t oc_clock_seconds_v1(). This change allows
the representation of UNIX Epoch times beyond the year 2038, which
would otherwise be limited by the 32-bit integer size of long.

Fixes coverity issue:
55774: Use of 32-bit time_t
  • Loading branch information
Danielius1922 committed Jul 7, 2023
1 parent 1f6dc33 commit 3148d4a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 15 deletions.
1 change: 1 addition & 0 deletions api/unittest/plgdtimetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ TEST_F(TestPlgdTime, Seconds)
oc_clock_time_t start_ts = start / OC_CLOCK_SECOND;
EXPECT_LE(start_ts, now_ts);
EXPECT_GT(oc_clock_seconds(), now_ts);
EXPECT_GT(oc_clock_seconds_v1(), now_ts);
}

TEST_F(TestPlgdTime, IsActive)
Expand Down
10 changes: 8 additions & 2 deletions port/android/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void)
(oc_clock_time_t)ceil((double)t.tv_nsec / (1.e09 / OC_CLOCK_SECOND));
}

unsigned long
oc_clock_seconds(void)
uint64_t
oc_clock_seconds_v1(void)
{
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
Expand All @@ -66,6 +66,12 @@ oc_clock_seconds(void)
return 0;
}

unsigned long
oc_clock_seconds(void)
{
return (unsigned long)oc_clock_seconds_v1();
}

void
oc_clock_wait(oc_clock_time_t t)
{
Expand Down
8 changes: 7 additions & 1 deletion port/arduino/adapter/oc_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ oc_clock_time_monotonic(void)
return oc_clock_time();
}

uint64_t
oc_clock_seconds_v1(void)
{
return (uint64_t)oc_clock_seconds_v1();
}

unsigned long
oc_clock_seconds(void)
{
return (unsigned long)secondNow();
return (unsigned long)oc_clock_seconds_v1();
}

void
Expand Down
10 changes: 8 additions & 2 deletions port/esp32/adapter/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void)
(double)(esp_timer_get_time() / (1.e06 / OC_CLOCK_SECOND)));
}

unsigned long
oc_clock_seconds(void)
uint64_t
oc_clock_seconds_v1(void)
{
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
Expand All @@ -66,6 +66,12 @@ oc_clock_seconds(void)
return 0;
}

unsigned long
oc_clock_seconds(void)
{
return (unsigned long)oc_clock_seconds_v1();
}

void
oc_clock_wait(oc_clock_time_t t)
{
Expand Down
10 changes: 8 additions & 2 deletions port/linux/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ oc_clock_time_monotonic(void)
(oc_clock_time_t)ceil((double)t.tv_nsec / (1.e09 / OC_CLOCK_SECOND));
}

unsigned long
oc_clock_seconds(void)
uint64_t
oc_clock_seconds_v1(void)
{
struct timespec t;
if (clock_gettime(CLOCK_REALTIME, &t) != -1) {
Expand All @@ -66,6 +66,12 @@ oc_clock_seconds(void)
return 0;
}

unsigned long
oc_clock_seconds(void)
{
return (unsigned long)oc_clock_seconds_v1();
}

void
oc_clock_wait(oc_clock_time_t t)
{
Expand Down
17 changes: 14 additions & 3 deletions port/oc_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

#include "oc_clock_util.h"
#include "oc_export.h"
#include "util/oc_compiler.h"

#include <stdbool.h>
#include <stdint.h>

Expand Down Expand Up @@ -121,13 +123,22 @@ oc_clock_time_t oc_clock_time_monotonic(void);
/**
* Get the current value of the platform seconds.
*
* This could be the number of seconds since startup, or
* since a standard epoch.
* This could be the number of seconds since startup, or since a standard epoch.
*
* \return The value.
*/
OC_API
unsigned long oc_clock_seconds(void);
uint64_t oc_clock_seconds_v1(void);

/**
* Get the current value of the platform seconds.
* @deprecated Use oc_clock_seconds_v1 instead because it returns a 64-bit value
* on all platforms and thus is safe for years 2038+.
*/
OC_API
unsigned long oc_clock_seconds(void)
OC_DEPRECATED("replaced by oc_clock_seconds_v1 in v2.2.5.7");

/**
* Wait for a given number of ticks.
Expand Down
11 changes: 8 additions & 3 deletions port/openthread/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ oc_clock_time_monotonic(void)
return oc_clock_time();
}

uint64_t
oc_clock_seconds_v1(void)
{
uint64_t time = oc_clock_time() / OC_CLOCK_SECOND;
return time;
}

unsigned long
oc_clock_seconds(void)
{
unsigned long time = oc_clock_time() / OC_CLOCK_SECOND;

return time;
return (unsigned long)oc_clock_seconds_v1();
}

void
Expand Down
8 changes: 7 additions & 1 deletion port/windows/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,16 @@ oc_clock_time_monotonic(void)
return whole + part;
}

uint64_t
oc_clock_seconds_v1(void)
{
return (uint64_t)time(0);
}

unsigned long
oc_clock_seconds(void)
{
return (unsigned long)time(0);
return (unsigned long)oc_clock_seconds_v1();
}

void
Expand Down
8 changes: 7 additions & 1 deletion port/zephyr/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ oc_clock_time_monotonic(void)
return oc_clock_time();
}

uint64_t
oc_clock_seconds_v1(void)
{
return (oc_clock_time() + OC_CLOCK_SECOND - 1) / OC_CLOCK_SECOND;
}

unsigned long
oc_clock_seconds(void)
{
return (oc_clock_time() + OC_CLOCK_SECOND - 1) / OC_CLOCK_SECOND;
return (unsigned long)oc_clock_seconds_v1();
}

void
Expand Down
2 changes: 2 additions & 0 deletions swig/swig_interfaces/oc_clock.i
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ typedef long long oc_clock_time_t;
%rename(clockTime) oc_clock_time;
%rename(clockTimeMonotonic) oc_clock_time_monotonic;
%rename(clockSeconds) oc_clock_seconds;
%rename(clockSecondsV1) oc_clock_seconds_v1;
%rename(clockWait) oc_clock_wait;

#define OC_API
#define OC_DEPRECATED(...)
#define OC_NONNULL(...)
%include "port/oc_clock.h"

0 comments on commit 3148d4a

Please sign in to comment.