BUG/MINOR: time: fix NS_TO_TV macro

NS_TO_TV helper was implemented in 591fa59 ("MINOR: time: add conversions
to/from nanosecond timestamps")

Due to NS_TO_TV being implemented as a macro and not a function, we must
take extra care when manipulating user input.

In current implementation, 't' argument is not isolated within the macro.

Because of this, NS_TO_TV(1 + 1) will expand to:
  ((const struct timeval){ .tv_sec = 1 + 1 / 1000000000ULL, .tv_usec = (1 + 1 % 1000000000ULL) / 1000U })

Instead of:
  ((const struct timeval){ .tv_sec = 2 / 1000000000ULL, .tv_usec = (2 % 1000000000ULL) / 1000U })

As such, NS_TO_TV usage in hlua_now() is currently incorrect and this
results in unexpected values being passed to lua.

In this patch, we're adding an extra parenthesis around 't' in NS_TO_TV()
macro to make it safe against such usages. (that is: ensure proper
argument expansion as if NS_TO_TV was implemented as a function)

This is a 2.8 specific bug, no backport needed.
This commit is contained in:
Aurelien DARRAGON 2023-05-04 17:27:07 +02:00 committed by Willy Tarreau
parent 34f81d5815
commit e910909556

View File

@ -153,7 +153,7 @@ static forceinline ullong us_to_ns(uint us)
}
/* creates a struct timeval from a relative timestamp in nanosecond */
#define NS_TO_TV(t) ((const struct timeval){ .tv_sec = t / 1000000000ULL, .tv_usec = (t % 1000000000ULL) / 1000U })
#define NS_TO_TV(t) ((const struct timeval){ .tv_sec = (t) / 1000000000ULL, .tv_usec = ((t) % 1000000000ULL) / 1000U })
/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
* usecs between 0 and 999999. This function has been optimized to remove