From: Aurelien DARRAGON Date: Thu, 4 May 2023 15:27:07 +0000 (+0200) Subject: BUG/MINOR: time: fix NS_TO_TV macro X-Git-Tag: v2.8-dev10~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e9109095564cb17ced83ce5eb243f628021ba1e4;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/include/haproxy/time.h b/include/haproxy/time.h index f3d73893a0..3ebc683e1b 100644 --- a/include/haproxy/time.h +++ b/include/haproxy/time.h @@ -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