Krystian Hebel has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/70106 )
Change subject: commonlib/helpers.h: use unsigned literals in definitions ......................................................................
commonlib/helpers.h: use unsigned literals in definitions
This protects against some of unintentional integer promotions, e.g.:
uint16_t freq_mhz = 2148; // or bigger uint64_t freq = freq_mhz * MHz;
In this example, MHz used to be treated as int32_t, and because int32_t can represent every value that uin16_t can, multiplication was being performed with both arguments treated as int32_t. During assignment, result of multiplication is promoted to int64_t (because it can represent each value that int32_t can), and finally implicitly converted to uint64_t.
Promotions preserve the value, including the sign, so if result of multiplication is negative, the same negative number (but extended to 64 bits) is converted to unsigned number.
Signed-off-by: Krystian Hebel krystian.hebel@3mdeb.com Change-Id: Ie60a6ee82db80328e44639175272cc8097f36c3b --- M src/commonlib/bsd/include/commonlib/bsd/helpers.h 1 file changed, 32 insertions(+), 6 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/06/70106/1
diff --git a/src/commonlib/bsd/include/commonlib/bsd/helpers.h b/src/commonlib/bsd/include/commonlib/bsd/helpers.h index 4995305..73cd25f 100644 --- a/src/commonlib/bsd/include/commonlib/bsd/helpers.h +++ b/src/commonlib/bsd/include/commonlib/bsd/helpers.h @@ -72,13 +72,13 @@ } while (0)
/* Standard units. */ -#define KiB (1<<10) -#define MiB (1<<20) -#define GiB (1<<30) +#define KiB (1U<<10) +#define MiB (1U<<20) +#define GiB (1U<<30)
-#define KHz (1000) -#define MHz (1000 * KHz) -#define GHz (1000 * MHz) +#define KHz (1000U) +#define MHz (1000U * KHz) +#define GHz (1000U * MHz)
#ifndef offsetof #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)