From 42ed1c3fe60330c1ce98df1f6668bb8b4b2ded76 Mon Sep 17 00:00:00 2001 From: "Neil.wrz" Date: Thu, 8 Sep 2022 01:00:49 -0700 Subject: [PATCH 5/9] refactor handle warnings Signed-off-by: Neil.wrz --- src/conf.c | 11 ++++++++--- src/lcrcontainer.c | 8 +++++--- src/lcrcontainer_execute.c | 30 +++++++++++++++++++++--------- src/lcrcontainer_extend.c | 7 ++++--- src/utils.c | 6 +++--- src/utils.h | 2 +- third_party/libocispec/read_file.c | 1 - third_party/log.c | 5 ++--- 8 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/conf.c b/src/conf.c index f2569b3b..4f644d98 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1391,7 +1391,8 @@ static int trans_resources_mem_swap_v1(const defs_resources *res, struct lcr_lis } } - if (res->memory->swappiness != -1) { + // int64: swappiness should be int64 + if (res->memory->swappiness != (uint64_t)-1) { /* set swappiness parameter of vmscan */ nret = trans_conf_uint64(conf, "lxc.cgroup.memory.swappiness", res->memory->swappiness); if (nret < 0) { @@ -2085,7 +2086,7 @@ static int trans_resources_cpu_weight_v2(const defs_resources *res, struct lcr_l return -1; } - if (trans_conf_int64(conf, "lxc.cgroup2.cpu.weight", lcr_util_trans_cpushare_to_cpuweight(res->cpu->shares)) != 0) { + if (trans_conf_int64(conf, "lxc.cgroup2.cpu.weight", lcr_util_trans_cpushare_to_cpuweight((int64_t)res->cpu->shares)) != 0) { return -1; } @@ -2227,13 +2228,17 @@ static int trans_io_bfq_weight_v2(const defs_resources_block_io *block_io, struc size_t len = block_io->weight_device_len; if (block_io->weight != INVALID_INT) { + if (block_io->weight < 10 || block_io->weight > 1000) { + ERROR("invalid io weight %d out of range [10-1000]", block_io->weight); + return -1; + } weight = lcr_util_trans_blkio_weight_to_io_bfq_weight(block_io->weight); if (weight < CGROUP2_BFQ_WEIGHT_MIN || weight > CGROUP2_BFQ_WEIGHT_MAX) { ERROR("invalid io weight cased by invalid blockio weight %d", block_io->weight); return -1; } - if (trans_conf_int(conf, "lxc.cgroup2.io.bfq.weight", weight) != 0) { + if (trans_conf_uint64(conf, "lxc.cgroup2.io.bfq.weight", weight) != 0) { return -1; } } diff --git a/src/lcrcontainer.c b/src/lcrcontainer.c index 5746b092..14cc6c43 100644 --- a/src/lcrcontainer.c +++ b/src/lcrcontainer.c @@ -498,11 +498,11 @@ bool lcr_kill(const char *name, const char *lcrpath, uint32_t signal) sret = kill(pid, (int)signal); if (sret < 0) { if (errno == ESRCH) { - WARN("Can not kill process (pid=%d) with signal %d for container: no such process", pid, signal); + WARN("Can not kill process (pid=%ld) with signal %u for container: no such process", (long)pid, (unsigned int)signal); ret = true; goto out_put; } - ERROR("Can not kill process (pid=%d) with signal %d for container", pid, signal); + ERROR("Can not kill process (pid=%ld) with signal %u for container", (long)pid, (unsigned int)signal); goto out_put; } @@ -999,6 +999,7 @@ static char *lcr_get_config_item(struct lxc_container *c, const char *key, bool char *cret = NULL; size_t len = 0; int nret = 0; + int config_item = 0; if (key == NULL) { ERROR("Key cannot be NULL"); @@ -1032,7 +1033,8 @@ static char *lcr_get_config_item(struct lxc_container *c, const char *key, bool goto out; } - if ((size_t)c->get_config_item(c, key, cret, (int)len + 1) != len) { + config_item = c->get_config_item(c, key, cret, (int)len + 1); + if (config_item < 0 || (size_t)config_item != len) { free(cret); cret = NULL; } diff --git a/src/lcrcontainer_execute.c b/src/lcrcontainer_execute.c index 042ad2fd..118f26c2 100644 --- a/src/lcrcontainer_execute.c +++ b/src/lcrcontainer_execute.c @@ -199,7 +199,7 @@ static int update_resources_cpu_weight_v2(struct lxc_container *c, const struct } int num = snprintf(numstr, sizeof(numstr), "%llu", - (unsigned long long)lcr_util_trans_cpushare_to_cpuweight(cr->cpu_shares)); + (unsigned long long)lcr_util_trans_cpushare_to_cpuweight((int64_t)cr->cpu_shares)); if (num < 0 || (size_t)num >= sizeof(numstr)) { return -1; } @@ -239,7 +239,7 @@ static int update_resources_cpu_max_v2(struct lxc_container *c, const struct lcr { int num = 0; uint64_t period = cr->cpu_period; - uint64_t quota = cr->cpu_quota; + int64_t quota = cr->cpu_quota; char numstr[128] = {0}; /* max buffer */ if (quota == 0 && period == 0) { @@ -252,8 +252,8 @@ static int update_resources_cpu_max_v2(struct lxc_container *c, const struct lcr // format: // $MAX $PERIOD - if ((int64_t) quota > 0) { - num = snprintf(numstr, sizeof(numstr), "%llu %llu", (unsigned long long)quota, (unsigned long long)period); + if (quota > 0) { + num = snprintf(numstr, sizeof(numstr), "%lld %llu", (long long int)quota, (unsigned long long)period); } else { num = snprintf(numstr, sizeof(numstr), "max %llu", (unsigned long long)period); } @@ -486,7 +486,7 @@ static int update_resources_memory_swap_v2(struct lxc_container *c, const struct return 0; } - if (lcr_util_get_real_swap(cr->memory_limit, cr->memory_swap, &swap) != 0) { + if (lcr_util_get_real_swap((int64_t)cr->memory_limit, (int64_t)cr->memory_swap, &swap) != 0) { return -1; } @@ -549,13 +549,15 @@ static bool update_resources_mem_v1(struct lxc_container *c, struct lcr_cgroup_r bool ret = false; // If the memory update is set to -1 we should also set swap to -1, it means unlimited memory. - if (cr->memory_limit == -1) { - cr->memory_swap = -1; + // int64 : memory_limit should be int64 + if (cr->memory_limit == (uint64_t)-1) { + cr->memory_swap = (uint64_t)-1; } + // int64 : memory_limit should be int64 if (cr->memory_limit != 0 && cr->memory_swap != 0) { uint64_t cur_mem_limit = stat_get_ull(c, "memory.limit_in_bytes"); - if (cr->memory_swap == -1 || cur_mem_limit < cr->memory_swap) { + if (cr->memory_swap == (uint64_t)-1 || cur_mem_limit < cr->memory_swap) { if (update_resources_memory_swap(c, cr) != 0) { goto err_out; } @@ -637,7 +639,12 @@ static int update_resources_io_weight_v2(struct lxc_container *c, const struct l return 0; } - weight = lcr_util_trans_blkio_weight_to_io_weight(cr->blkio_weight); + if (cr->blkio_weight < 10 || cr->blkio_weight > 1000) { + ERROR("invalid io weight %llu out of range [10-1000]", (unsigned long long)cr->blkio_weight); + return -1; + } + + weight = lcr_util_trans_blkio_weight_to_io_weight((int)cr->blkio_weight); if (weight < CGROUP2_WEIGHT_MIN || weight > CGROUP2_WEIGHT_MAX) { ERROR("invalid io weight cased by invalid blockio weight %llu", (unsigned long long) cr->blkio_weight); return -1; @@ -665,6 +672,11 @@ static int update_resources_io_bfq_weight_v2(struct lxc_container *c, const stru return 0; } + if (cr->blkio_weight < 10 || cr->blkio_weight > 1000) { + ERROR("invalid io weight %llu out of range [10-1000]", (unsigned long long)cr->blkio_weight); + return -1; + } + weight = lcr_util_trans_blkio_weight_to_io_bfq_weight(cr->blkio_weight); if (weight < CGROUP2_BFQ_WEIGHT_MIN || weight > CGROUP2_BFQ_WEIGHT_MAX) { ERROR("invalid io weight cased by invalid blockio weight %llu", (unsigned long long) cr->blkio_weight); diff --git a/src/lcrcontainer_extend.c b/src/lcrcontainer_extend.c index 717d13b4..e03c212a 100644 --- a/src/lcrcontainer_extend.c +++ b/src/lcrcontainer_extend.c @@ -818,6 +818,7 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf) lcr_list_for_each(it, lcr_conf) { lcr_config_item_t *item = it->elem; int nret; + size_t encode_len; if (item != NULL) { if (strlen(item->value) > ((SIZE_MAX - strlen(item->name)) - 4)) { goto cleanup; @@ -842,10 +843,10 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf) goto cleanup; } - nret = strlen(line_encode); + encode_len = strlen(line_encode); - line_encode[nret] = '\n'; - if (write(fd, line_encode, nret + 1) == -1) { + line_encode[encode_len] = '\n'; + if (write(fd, line_encode, encode_len + 1) == -1) { SYSERROR("Write failed"); goto cleanup; } diff --git a/src/utils.c b/src/utils.c index 7ee9ba81..16719f67 100644 --- a/src/utils.c +++ b/src/utils.c @@ -888,7 +888,7 @@ restart: } directory = opendir("/proc/self/fd"); if (directory == NULL) { - WARN("Failed to open directory: %m."); + WARN("Failed to open directory: /proc/self/fd."); return -1; } @@ -1307,10 +1307,10 @@ uint64_t lcr_util_trans_blkio_weight_to_io_weight(int weight) return (uint64_t)(1 + ((uint64_t)weight - 10) * 9999 / 990); } -uint64_t lcr_util_trans_blkio_weight_to_io_bfq_weight(int weight) +uint64_t lcr_util_trans_blkio_weight_to_io_bfq_weight(uint64_t weight) { // map from [10-1000] to [1-1000] - return (uint64_t)(1 + ((uint64_t)weight - 10) * 999 / 990); + return (uint64_t)(1 + (weight - 10) * 999 / 990); } int lcr_util_get_cgroup_version() diff --git a/src/utils.h b/src/utils.h index 5aae95da..865b899a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -219,7 +219,7 @@ int lcr_util_atomic_write_file(const char *filepath, const char *content); int lcr_util_get_real_swap(int64_t memory, int64_t memory_swap, int64_t *swap); int lcr_util_trans_cpushare_to_cpuweight(int64_t cpu_share); uint64_t lcr_util_trans_blkio_weight_to_io_weight(int weight); -uint64_t lcr_util_trans_blkio_weight_to_io_bfq_weight(int weight); +uint64_t lcr_util_trans_blkio_weight_to_io_bfq_weight(uint64_t weight); int lcr_util_get_cgroup_version(); #ifdef __cplusplus diff --git a/third_party/libocispec/read_file.c b/third_party/libocispec/read_file.c index 42b83b20..29dbec33 100644 --- a/third_party/libocispec/read_file.c +++ b/third_party/libocispec/read_file.c @@ -29,7 +29,6 @@ #endif #define JSON_MAX_SIZE (10LL * 1024LL * 1024LL) -#define FILE_MODE 0640 static int do_check_fread_args(const FILE *stream, const size_t *length) { diff --git a/third_party/log.c b/third_party/log.c index 2fcb014d..c3c19815 100644 --- a/third_party/log.c +++ b/third_party/log.c @@ -25,7 +25,6 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif -#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ #include #include #include @@ -267,7 +266,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender, if (ret < 0) return 0; - n += ret; + n += (int)ret; } if ((size_t)n >= sizeof(buffer)) @@ -275,7 +274,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender, buffer[n] = '\n'; - return lcr_util_write_nointr(fd_to_use, buffer, n + 1); + return lcr_util_write_nointr(fd_to_use, buffer, (size_t)n + 1); } static struct lxc_log_appender log_appender_stderr = { -- 2.25.1