diff --git a/src/lxc/confile.c b/src/lxc/confile.c index bf0f10008c..071ada7924 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; @@ -4726,9 +4726,9 @@ static int get_config_time_offset_boot(const char *key, char *retv, int inlen, s memset(retv, 0, inlen); if (c->timens.s_boot) { - strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot); + strprint(retv, inlen, "%" PRId64 "s", c->timens.s_boot); } else { - strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot); + strprint(retv, inlen, "%" PRId64 "ns", c->timens.ns_boot); } return fulllen; @@ -4746,9 +4746,9 @@ static int get_config_time_offset_monotonic(const char *key, char *retv, int inl memset(retv, 0, inlen); if (c->timens.s_monotonic) { - strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic); + strprint(retv, inlen, "%" PRId64 "s", c->timens.s_monotonic); } else { - strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic); + strprint(retv, inlen, "%" PRId64 "ns", c->timens.ns_monotonic); } return fulllen; diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 616544dd48..0b14c05ad8 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 4040f9f1e8..956ea2a5e9 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, diff --git a/src/tests/parse_config_file.c b/src/tests/parse_config_file.c index d0a285c1dd..9dd081168c 100644 --- a/src/tests/parse_config_file.c +++ b/src/tests/parse_config_file.c @@ -906,12 +906,12 @@ int main(int argc, char *argv[]) } if (c->set_config_item(c, "lxc.hook.version", "2")) { - lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.hook.version\" to \"2\""); + lxc_error("%s\n", "Managed to set invalid config item \"lxc.hook.version\" to \"2\""); goto non_test_error; } if (!c->set_config_item(c, "lxc.monitor.signal.pdeath", "SIGKILL")) { - lxc_error("%s\n", "Failed to set to set invalid config item \"lxc.monitor.signal.pdeath\" to \"SIGKILL\""); + lxc_error("%s\n", "Failed to set invalid config item \"lxc.monitor.signal.pdeath\" to \"SIGKILL\""); goto non_test_error; } @@ -921,17 +921,17 @@ int main(int argc, char *argv[]) } if (c->set_config_item(c, "lxc.notaconfigkey", "invalid")) { - lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.notaconfigkey\" to \"invalid\""); + lxc_error("%s\n", "Managed to set invalid config item \"lxc.notaconfigkey\" to \"invalid\""); return -1; } if (c->set_config_item(c, "lxc.log.file=", "./")) { - lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.log.file\" to \"./\""); + lxc_error("%s\n", "Managed to set invalid config item \"lxc.log.file\" to \"./\""); return -1; } if (c->set_config_item(c, "lxc.hook.versionasdfsadfsadf", "1")) { - lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.hook.versionasdfsadfsadf\" to \"2\""); + lxc_error("%s\n", "Managed to set invalid config item \"lxc.hook.versionasdfsadfsadf\" to \"2\""); goto non_test_error; } @@ -940,6 +940,31 @@ int main(int argc, char *argv[]) goto non_test_error; } + if (set_get_compare_clear_save_load(c, "lxc.time.offset.boot", "-1234s", tmpf, true) < 0) { + lxc_error("%s\n", "lxc.time.offset.boot"); + goto non_test_error; + } + + if (!c->set_config_item(c, "lxc.time.offset.boot", "1000000000000000us")) { + lxc_error("%s\n", "Failed to set a valid value (1000000000000000us) to config item \"lxc.time.offset.boot\""); + goto non_test_error; + } + + if (!c->set_config_item(c, "lxc.time.offset.boot", "-1000000000000000us")) { + lxc_error("%s\n", "Failed to set a valid (-1000000000000000us) value to config item \"lxc.time.offset.boot\""); + goto non_test_error; + } + + if (c->set_config_item(c, "lxc.time.offset.boot", "10000000000000000us")) { + lxc_error("%s\n", "Managed to set overflowed value to config item \"lxc.time.offset.boot\""); + goto non_test_error; + } + + if (set_get_compare_clear_save_load(c, "lxc.time.offset.monotonic", "4321s", tmpf, true) < 0) { + lxc_error("%s\n", "lxc.time.offset.monotonic"); + goto non_test_error; + } + fret = EXIT_SUCCESS; non_test_error: