From 587a39b7290b8437dbc15edbf679f33d72459df0 Mon Sep 17 00:00:00 2001 From: Richard Yao Date: Tue, 29 Nov 2022 12:53:33 -0500 Subject: [PATCH] Lua: Fix bad bitshift in lua_strx2number() The port of lua to OpenZFS modified lua to use int64_t for numbers instead of double. As part of this, a function for calculating exponentiation was replaced with a bit shift. Unfortunately, it did not handle negative values. Also, it only supported exponents numbers with 7 digits before before overflow. This supports exponents up to 15 digits before overflow. Clang's static analyzer reported this as "Result of operation is garbage or undefined" because the exponent was negative. Reviewed-by: Damian Szuberski Reviewed-by: Brian Behlendorf Signed-off-by: Richard Yao Closes #14204 --- module/lua/lobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module/lua/lobject.c b/module/lua/lobject.c index f74dacdf5f..ea1f9a8e3b 100644 --- a/module/lua/lobject.c +++ b/module/lua/lobject.c @@ -143,7 +143,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { *endptr = cast(char *, s); /* valid up to here */ ret: if (neg) r = -r; - return (r * (1 << e)); + return ((e >= 0) ? (r * (1ULL << e)) : (r / (1ULL << -e))); } #endif