From: Lennart Poettering Date: Fri, 29 Oct 2021 12:33:37 +0000 (+0200) Subject: percent-util: clamp percent range before converting to 2^32 scale X-Git-Tag: v250-rc1~366 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2f2cdd7250d83af27820d0223ed588ad7b02eb71;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/basic/percent-util.h b/src/basic/percent-util.h index 24f4c3bdeff..e975d6e3e3c 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) {