From 2f2cdd7250d83af27820d0223ed588ad7b02eb71 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 29 Oct 2021 14:33:37 +0200 Subject: [PATCH] percent-util: clamp percent range before converting to 2^32 scale Let#s better be safe than sorry and clamp the input, so that we don't hit overflow issues. --- src/basic/percent-util.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/basic/percent-util.h b/src/basic/percent-util.h index 24f4c3bdef..e975d6e3e3 100644 --- a/src/basic/percent-util.h +++ b/src/basic/percent-util.h @@ -19,15 +19,16 @@ int parse_permyriad(const char *p); * a value relative to 100% == 2^32-1. Rounds to closest. */ static inline uint32_t UINT32_SCALE_FROM_PERCENT(int percent) { assert_cc(INT_MAX <= UINT32_MAX); - return (uint32_t) (((uint64_t) percent * UINT32_MAX + 50) / 100U); + + return (uint32_t) (((uint64_t) CLAMP(percent, 0, 100) * UINT32_MAX + 50) / 100U); } static inline uint32_t UINT32_SCALE_FROM_PERMILLE(int permille) { - return (uint32_t) (((uint64_t) permille * UINT32_MAX + 500) / 1000U); + return (uint32_t) (((uint64_t) CLAMP(permille, 0, 1000) * UINT32_MAX + 500) / 1000U); } static inline uint32_t UINT32_SCALE_FROM_PERMYRIAD(int permyriad) { - return (uint32_t) (((uint64_t) permyriad * UINT32_MAX + 5000) / 10000U); + return (uint32_t) (((uint64_t) CLAMP(permyriad, 0, 10000) * UINT32_MAX + 5000) / 10000U); } static inline int UINT32_SCALE_TO_PERCENT(uint32_t scale) { -- 2.25.1