]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tree-wide: replace multiply_overflow with check_mul_overflow
authorArnaud Fontaine <arnaud.fontaine@airbus.com>
Tue, 2 Apr 2024 08:49:34 +0000 (10:49 +0200)
committerAlexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Tue, 2 Apr 2024 10:22:16 +0000 (12:22 +0200)
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 <arnaud.fontaine@airbus.com>
Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
src/lxc/confile.c
src/lxc/utils.c
src/lxc/utils.h

index bf0f10008cfdd67aeaf7b2f63c4e48c2ab4570b7..defb5ed965c8c8bdb140112143daec5e3ebc8cd8 100644 (file)
@@ -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;
index 616544dd489c0cb3656b76f91642f99ff5ef295b..0b14c05ad80cb69b1a11c199b815f34e8d59b27e 100644 (file)
@@ -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;
index 4040f9f1e8b5e7fa9b387aa555bce7cea4e663a4..956ea2a5e9e12c89374a0772815ba99007aa4a98 100644 (file)
@@ -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,