From be7c7aa0408e7ed4d37a19a98a84c08e74f51a26 Mon Sep 17 00:00:00 2001 From: Philip Elcan Date: Wed, 5 Jun 2019 14:51:32 -0400 Subject: [PATCH] gpu: nvgpu: posix: make round*_pow_of_two CERT-C friendly CERT-C Rule INT30 requires checking unsigned arithmetic for potential wrap. The macros roundup_pow_of_two and rounddown_pow_of_two could potentially wrap if the passed parameter were 0. JIRA NVGPU-3586 Change-Id: I9eba4c197b74db555055e1199ce72131b071062c Signed-off-by: Philip Elcan Reviewed-on: https://git-master.nvidia.com/r/2131157 GVS: Gerrit_Virtual_Submit Reviewed-by: Nitin Kumbhar Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/include/nvgpu/posix/log2.h | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h b/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h index 77d69847a..ee5d47cd0 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h @@ -33,8 +33,29 @@ fls_val; \ }) -#define roundup_pow_of_two(x) (1UL << fls((x) - 1UL)) -#define rounddown_pow_of_two(x) (1UL << (fls(x) - 1UL)) +#define roundup_pow_of_two(x) \ + ({ \ + unsigned long ret; \ + \ + if ((x) == 0UL) { \ + BUG(); \ + } else { \ + ret = 1UL << fls((x) - 1UL); \ + } \ + ret; \ + }) + +#define rounddown_pow_of_two(x) \ + ({ \ + unsigned long ret; \ + \ + if ((x) == 0UL) { \ + BUG(); \ + } else { \ + ret = 1UL << (fls(x) - 1UL); \ + } \ + ret; \ + }) #define is_power_of_2(x) \ (bool)({ \