Skip to content

Commit

Permalink
Merge pull request #131 from bosch-io/fix_32bit_time_overflow
Browse files Browse the repository at this point in the history
dtls.c, netq.c: consider 32bit time overflow.
  • Loading branch information
obgm authored May 23, 2022
2 parents 92ca636 + cde751a commit c3754dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -4702,7 +4702,8 @@ dtls_check_retransmit(dtls_context_t *context, clock_time_t *next) {
netq_t *node = netq_head(&context->sendqueue);

dtls_ticks(&now);
while (node && node->t <= now) {
/* comparison considering 32bit overflow */
while (node && DTLS_IS_BEFORE_TIME(node->t, now)) {
netq_pop_first(&context->sendqueue);
dtls_retransmit(context, node);
node = netq_head(&context->sendqueue);
Expand Down
7 changes: 7 additions & 0 deletions dtls_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#endif

typedef uint64_t clock_time_t;

#else /* WITH_CONTIKI || RIOT_VERSION */

#ifdef HAVE_TIME_H
Expand All @@ -62,6 +63,7 @@ typedef uint64_t clock_time_t;
#endif

typedef uint32_t clock_time_t;

#endif /* WITH_CONTIKI || RIOT_VERSION */

typedef clock_time_t dtls_tick_t;
Expand All @@ -73,6 +75,11 @@ typedef clock_time_t dtls_tick_t;
void dtls_clock_init(void);
void dtls_ticks(dtls_tick_t *t);

/* see https://godbolt.org/z/YchexKaeT */
#define DTLS_OFFSET_TIME (((clock_time_t)~0) >> 1)
/** Checks if A is before (or equal) B. Considers 32 bit time overflow */
#define DTLS_IS_BEFORE_TIME(A, B) ((clock_time_t)(DTLS_OFFSET_TIME + (B)-(A)) >= DTLS_OFFSET_TIME)

/** @} */

#endif /* _DTLS_DTLS_TIME_H_ */
3 changes: 2 additions & 1 deletion netq.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ netq_insert_node(netq_t **queue, netq_t *node) {
assert(node);

p = *queue;
while(p && p->t <= node->t) {
/* comparison considering 32bit overflow */
while(p && DTLS_IS_BEFORE_TIME(p->t, node->t)) {
assert(p != node);
if (p == node)
return 0;
Expand Down

0 comments on commit c3754dc

Please sign in to comment.