diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-18 11:10:50 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-10-19 11:31:44 +0200 |
commit | ffee7b97e3009d7d7370c80da42af766cc718c72 (patch) | |
tree | 6bf3af2b910b05ac1b5747c314383ef45444d907 /src/basic/macro.h | |
parent | sd-journal: use PAGE_ALIGN_U64() and friends (diff) | |
download | systemd-ffee7b97e3009d7d7370c80da42af766cc718c72.tar.xz systemd-ffee7b97e3009d7d7370c80da42af766cc718c72.zip |
macro: introduce u64_multiply_safe() to avoid overflow
Just a paranoia.
Diffstat (limited to 'src/basic/macro.h')
-rw-r--r-- | src/basic/macro.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/basic/macro.h b/src/basic/macro.h index dcd1ca96fd..d3eb980abd 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -51,6 +51,13 @@ #error "neither int nor long are four bytes long?!?" #endif +static inline uint64_t u64_multiply_safe(uint64_t a, uint64_t b) { + if (_unlikely_(a != 0 && b > (UINT64_MAX / a))) + return 0; /* overflow */ + + return a * b; +} + /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */ static inline unsigned long ALIGN_POWER2(unsigned long u) { |