From: Arnaud Fontaine Date: Tue, 2 Apr 2024 08:49:34 +0000 (+0200) Subject: tree-wide: replace multiply_overflow with check_mul_overflow X-Git-Tag: v6.0.0~6^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ad8f554458d1e93bd5dbde7ed650cb24a5401ce3;p=thirdparty%2Flxc.git tree-wide: replace multiply_overflow with check_mul_overflow Remove redundant multiply_overflow helper and use check_mul_overflow. This also fixes a bug with incorrect handling for negative offset values. Link: #4374 Signed-off-by: Arnaud Fontaine Signed-off-by: Alexander Mikhalitsyn --- diff --git a/src/lxc/confile.c b/src/lxc/confile.c index bf0f10008..defb5ed96 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -2963,18 +2963,18 @@ static int set_config_time_offset_boot(const char *key, const char *value, unit = lxc_trim_whitespace_in_place(buf); if (strequal(unit, "h")) { - if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot)) + if (check_mul_overflow(offset, (typeof(offset))3600, &lxc_conf->timens.s_boot)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "m")) { - if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot)) + if (check_mul_overflow(offset, (typeof(offset))60, &lxc_conf->timens.s_boot)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "s")) { lxc_conf->timens.s_boot = offset; } else if (strequal(unit, "ms")) { - if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot)) + if (check_mul_overflow(offset, (typeof(offset))1000000, &lxc_conf->timens.ns_boot)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "us")) { - if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot)) + if (check_mul_overflow(offset, (typeof(offset))1000, &lxc_conf->timens.ns_boot)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "ns")) { lxc_conf->timens.ns_boot = offset; @@ -3002,18 +3002,18 @@ static int set_config_time_offset_monotonic(const char *key, const char *value, unit = lxc_trim_whitespace_in_place(buf); if (strequal(unit, "h")) { - if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic)) + if (check_mul_overflow(offset, (typeof(offset))3600, &lxc_conf->timens.s_monotonic)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "m")) { - if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic)) + if (check_mul_overflow(offset, (typeof(offset))60, &lxc_conf->timens.s_monotonic)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "s")) { lxc_conf->timens.s_monotonic = offset; } else if (strequal(unit, "ms")) { - if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic)) + if (check_mul_overflow(offset, (typeof(offset))1000000, &lxc_conf->timens.ns_monotonic)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "us")) { - if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic)) + if (check_mul_overflow(offset, (typeof(offset))1000, &lxc_conf->timens.ns_monotonic)) return ret_errno(EOVERFLOW); } else if (strequal(unit, "ns")) { lxc_conf->timens.ns_monotonic = offset; diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 616544dd4..0b14c05ad 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2071,18 +2071,6 @@ int fix_stdio_permissions(uid_t uid) return fret; } -bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res) -{ - if (base > 0 && base > (int64_t)(INT64_MAX / mult)) - return false; - - if (base < 0 && base < (int64_t)(INT64_MIN / mult)) - return false; - - *res = (int64_t)(base * mult); - return true; -} - int print_r(int fd, const char *path) { __do_close int dfd = -EBADF, dfd_dup = -EBADF; diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 4040f9f1e..956ea2a5e 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -240,8 +240,6 @@ static inline bool gid_valid(gid_t gid) return gid != LXC_INVALID_GID; } -__hidden extern bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res); - __hidden extern int safe_mount_beneath(const char *beneath, const char *src, const char *dst, const char *fstype, unsigned int flags, const void *data); __hidden extern int safe_mount_beneath_at(int beneat_fd, const char *src, const char *dst,