Prevent interger overflow on the AVR

Compiling examples/er-rest-example for the avr-raven gives:

  integer overflow in expression [-Woverflow]

On the AVR int is 16 bit and some calculations may overflow.
This happens for example with multiplications involving
CLOCK_SECOND where all the operands are of type int.
Casting one of the operands to clock_time_t forces
the arithmetic to clock_time_t without increasing the size
of the program (the calculation is performed at compile time).
This commit is contained in:
Cristiano De Alti 2016-11-05 22:46:42 +01:00
parent 1ebceb8f23
commit 1bdec853e1

View File

@ -51,7 +51,7 @@
/* Maximum value for the freshness counter */ /* Maximum value for the freshness counter */
#define FRESHNESS_MAX 16 #define FRESHNESS_MAX 16
/* Statistics with no update in FRESHNESS_EXPIRATION_TIMEOUT is not fresh */ /* Statistics with no update in FRESHNESS_EXPIRATION_TIMEOUT is not fresh */
#define FRESHNESS_EXPIRATION_TIME (10 * 60 * CLOCK_SECOND) #define FRESHNESS_EXPIRATION_TIME (10 * 60 * (clock_time_t)CLOCK_SECOND)
/* EWMA (exponential moving average) used to maintain statistics over time */ /* EWMA (exponential moving average) used to maintain statistics over time */
#define EWMA_SCALE 100 #define EWMA_SCALE 100
@ -206,6 +206,6 @@ void
link_stats_init(void) link_stats_init(void)
{ {
nbr_table_register(link_stats, NULL); nbr_table_register(link_stats, NULL);
ctimer_set(&periodic_timer, 60 * CLOCK_SECOND * FRESHNESS_HALF_LIFE, ctimer_set(&periodic_timer, 60 * (clock_time_t)CLOCK_SECOND * FRESHNESS_HALF_LIFE,
periodic, NULL); periodic, NULL);
} }