From e29e6c1a51464a384b3317d4aad56aeef3221217 Mon Sep 17 00:00:00 2001 From: zhangxiaoyu Date: Sat, 26 Aug 2023 10:14:46 +0000 Subject: [PATCH 118/145] !2137 do clean code * do clean code --- src/cmd/isulad-shim/terminal.c | 6 +++-- src/daemon/common/sysinfo.c | 27 ++++++++++++------- src/daemon/config/isulad_config.c | 8 +++--- src/daemon/entry/cri/checkpoint_handler.cc | 2 +- src/daemon/entry/cri/checkpoint_handler.h | 1 + src/daemon/entry/cri/naming.cc | 2 +- .../entry/cri/websocket/service/ws_server.cc | 12 ++++----- .../entry/cri/websocket/service/ws_server.h | 2 +- .../executor/container_cb/execution_extend.c | 1 - src/daemon/modules/container/container_unix.c | 4 +-- src/daemon/modules/image/oci/oci_load.c | 2 +- .../graphdriver/devmapper/deviceset.c | 2 ++ .../graphdriver/overlay2/driver_overlay2.c | 3 +-- src/utils/cpputils/url.cc | 3 +++ src/utils/cpputils/url.h | 2 -- src/utils/cutils/utils_mount_spec.c | 2 +- src/utils/cutils/utils_timestamp.c | 15 +++++++---- src/utils/http/http.c | 4 +-- src/utils/http/parser.c | 10 +++---- src/utils/sha256/sha256.c | 16 +++++------ 20 files changed, 69 insertions(+), 55 deletions(-) diff --git a/src/cmd/isulad-shim/terminal.c b/src/cmd/isulad-shim/terminal.c index 23783244..43fb476f 100644 --- a/src/cmd/isulad-shim/terminal.c +++ b/src/cmd/isulad-shim/terminal.c @@ -186,10 +186,12 @@ static bool util_get_time_buffer(struct timespec *timestamp, char *timebuffer, s seconds = (time_t)timestamp->tv_sec; gmtime_r(&seconds, &tm_utc); - strftime(timebuffer, maxsize, "%Y-%m-%dT%H:%M:%S", &tm_utc); + len = strftime(timebuffer, maxsize, "%Y-%m-%dT%H:%M:%S", &tm_utc); + if (len == 0) { + return false; + } nanos = (int32_t)timestamp->tv_nsec; - len = strlen(timebuffer); ret = snprintf(timebuffer + len, (maxsize - len), ".%09dZ", nanos); if (ret < 0 || (size_t)ret >= (maxsize - len)) { return false; diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c index cb02bee3..d0927f58 100644 --- a/src/daemon/common/sysinfo.c +++ b/src/daemon/common/sysinfo.c @@ -142,6 +142,7 @@ static int add_null_to_list(void ***list) } } + // add 2 size for element and NULL if (index > SIZE_MAX / sizeof(void **) - 2) { ERROR("Out of range"); return -1; @@ -179,7 +180,7 @@ static int append_subsystem_to_list(char ***klist, char ***nlist, const char *pt { int ret = 0; - if (strncmp(ptoken, "name=", 5) == 0) { + if (strncmp(ptoken, "name=", strlen("name=")) == 0) { ret = append_string(nlist, ptoken); if (ret != 0) { ERROR("Failed to append string"); @@ -271,26 +272,26 @@ static bool list_contain_string(const char **a_list, const char *str) static char *cgroup_legacy_must_prefix_named(const char *entry) { - size_t len; + size_t entry_len; char *prefixed = NULL; const char *prefix = "name="; + const size_t prefix_len = strlen(prefix); - len = strlen(entry); - - if (((SIZE_MAX - len) - 1) < strlen(prefix)) { + entry_len = strlen(entry); + if ((SIZE_MAX - entry_len) < (prefix_len + 1)) { ERROR("Out of memory"); return NULL; } - prefixed = util_common_calloc_s(len + strlen(prefix) + 1); + prefixed = util_common_calloc_s(entry_len + prefix_len + 1); if (prefixed == NULL) { ERROR("Out of memory"); return NULL; } - (void)memcpy(prefixed, prefix, strlen(prefix)); - (void)memcpy(prefixed + strlen(prefix), entry, len); + (void)memcpy(prefixed, prefix, prefix_len); + (void)memcpy(prefixed + prefix_len, entry, entry_len); - prefixed[len + strlen(prefix)] = '\0'; + prefixed[entry_len + prefix_len] = '\0'; return prefixed; } @@ -310,7 +311,7 @@ static int append_controller(const char **klist, const char **nlist, char ***cli return -1; } - if (strncmp(entry, "name=", 5) == 0) { + if (strncmp(entry, "name=", strlen("name=")) == 0) { dup_entry = util_strdup_s(entry); } else if (list_contain_string(klist, entry)) { dup_entry = util_strdup_s(entry); @@ -345,6 +346,8 @@ static char **cgroup_get_controllers(const char **klist, const char **nlist, con char *sep = ","; char **pret = NULL; + // line example + // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755 for (index = 0; index < 4; index++) { pos = strchr(pos, ' '); if (pos == NULL) { @@ -413,6 +416,8 @@ int cgroup_get_mountpoint_and_root(char *pline, char **mountpoint, char **root) char *pos = pline; // find root + // line example + // 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755 for (index = 0; index < 3; index++) { pos = strchr(pos, ' '); if (pos == NULL) { @@ -1509,6 +1514,8 @@ void free_mount_info(mountinfo_t *info) free(info); } +// line example +// 108 99 0:55 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755 mountinfo_t *get_mount_info(const char *pline) { size_t length; diff --git a/src/daemon/config/isulad_config.c b/src/daemon/config/isulad_config.c index 17c9d3b5..6db4e2a4 100644 --- a/src/daemon/config/isulad_config.c +++ b/src/daemon/config/isulad_config.c @@ -297,6 +297,7 @@ char *conf_get_routine_rootdir(const char *runtime) char *path = NULL; struct service_arguments *conf = NULL; size_t len = 0; + size_t graph_len = 0; if (runtime == NULL) { ERROR("Runtime is NULL"); @@ -314,11 +315,12 @@ char *conf_get_routine_rootdir(const char *runtime) } /* path = conf->rootpath + / + engines + / + runtime + /0 */ - if (strlen(conf->json_confs->graph) > (SIZE_MAX - strlen(ENGINE_ROOTPATH_NAME) - strlen(runtime)) - 3) { + graph_len = strlen(conf->json_confs->graph); + if (graph_len > (SIZE_MAX - strlen(ENGINE_ROOTPATH_NAME) - strlen(runtime)) - 3) { ERROR("Graph path is too long"); goto out; } - len = strlen(conf->json_confs->graph) + 1 + strlen(ENGINE_ROOTPATH_NAME) + 1 + strlen(runtime) + 1; + len = graph_len + 1 + strlen(ENGINE_ROOTPATH_NAME) + 1 + strlen(runtime) + 1; if (len > PATH_MAX / sizeof(char)) { ERROR("The size of path exceeds the limit"); goto out; @@ -1685,13 +1687,13 @@ out: static bool valid_isulad_daemon_constants(isulad_daemon_constants *config) { json_map_string_string *registry_transformation = NULL; - size_t i = 0; if (config == NULL) { return false; } if (config->registry_transformation != NULL) { + size_t i; registry_transformation = config->registry_transformation; for (i = 0; i < registry_transformation->len; i++) { if (!util_valid_host_name(registry_transformation->keys[i]) || diff --git a/src/daemon/entry/cri/checkpoint_handler.cc b/src/daemon/entry/cri/checkpoint_handler.cc index d5eab7a7..7b5f49cc 100644 --- a/src/daemon/entry/cri/checkpoint_handler.cc +++ b/src/daemon/entry/cri/checkpoint_handler.cc @@ -15,7 +15,7 @@ #include "checkpoint_handler.h" #include -#include +#include #include #include #include diff --git a/src/daemon/entry/cri/checkpoint_handler.h b/src/daemon/entry/cri/checkpoint_handler.h index b526df0c..7510265f 100644 --- a/src/daemon/entry/cri/checkpoint_handler.h +++ b/src/daemon/entry/cri/checkpoint_handler.h @@ -48,6 +48,7 @@ private: class CheckpointData { public: + CheckpointData() = default; void CheckpointDataToCStruct(cri_checkpoint_data **data, Errors &error); void CStructToCheckpointData(const cri_checkpoint_data *data, Errors &error); diff --git a/src/daemon/entry/cri/naming.cc b/src/daemon/entry/cri/naming.cc index 54a14a81..1526f044 100644 --- a/src/daemon/entry/cri/naming.cc +++ b/src/daemon/entry/cri/naming.cc @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include "cri_constants.h" #include "cri_helpers.h" diff --git a/src/daemon/entry/cri/websocket/service/ws_server.cc b/src/daemon/entry/cri/websocket/service/ws_server.cc index ea320ff4..078c856c 100644 --- a/src/daemon/entry/cri/websocket/service/ws_server.cc +++ b/src/daemon/entry/cri/websocket/service/ws_server.cc @@ -326,7 +326,7 @@ void WebsocketServer::CloseWsSession(int socketID) }).detach(); } -int WebsocketServer::GenerateSessionData(SessionData *session, const std::string containerID) noexcept +int WebsocketServer::GenerateSessionData(SessionData *session, const std::string &containerID) noexcept { char *suffix = nullptr; int readPipeFd[2] = { -1, -1 }; @@ -467,7 +467,7 @@ void WebsocketServer::DumpHandshakeInfo(struct lws *wsi) noexcept lws_hdr_copy(wsi, buf, sizeof(buf), (lws_token_indexes)n); buf[sizeof(buf) - 1] = '\0'; - DEBUG(" %s = %s", (char *)c, buf); + DEBUG(" %s = %s", reinterpret_cast(const_cast(c)), buf); n++; } while (c != nullptr); } @@ -562,14 +562,14 @@ void WebsocketServer::Receive(int socketID, void *in, size_t len, bool complete) if (!it->second->IsStdinComplete()) { DEBUG("Receive remaning stdin data with length %zu", len); // Too much data may cause error 'resource temporarily unavaliable' by using 'write' - if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), (char *)in, len) < 0) { + if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), static_cast(in), len) < 0) { ERROR("Sub write over! err msg: %s", strerror(errno)); } goto out; } if (*static_cast(in) == WebsocketChannel::RESIZECHANNEL) { - if (ResizeTerminal(socketID, (char *)in + 1, len, it->second->containerID, it->second->suffix) != 0) { + if (ResizeTerminal(socketID, static_cast(in) + 1, len, it->second->containerID, it->second->suffix) != 0) { ERROR("Failed to resize terminal tty"); } if (!complete) { @@ -579,13 +579,13 @@ void WebsocketServer::Receive(int socketID, void *in, size_t len, bool complete) } if (*static_cast(in) == WebsocketChannel::STDINCHANNEL) { - if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), (char *)in + 1, len - 1) < 0) { + if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), static_cast(in) + 1, len - 1) < 0) { ERROR("Sub write over! err msg: %s", strerror(errno)); } goto out; } - ERROR("Invalid data: %s", (char *)in); + ERROR("Invalid data: %s", static_cast(in)); out: it->second->SetStdinComplete(complete); diff --git a/src/daemon/entry/cri/websocket/service/ws_server.h b/src/daemon/entry/cri/websocket/service/ws_server.h index 7da56818..f0e6dc02 100644 --- a/src/daemon/entry/cri/websocket/service/ws_server.h +++ b/src/daemon/entry/cri/websocket/service/ws_server.h @@ -79,7 +79,7 @@ private: int Wswrite(struct lws *wsi, const unsigned char *message); inline void DumpHandshakeInfo(struct lws *wsi) noexcept; int RegisterStreamTask(struct lws *wsi) noexcept; - int GenerateSessionData(SessionData *session, const std::string containerID) noexcept; + int GenerateSessionData(SessionData *session, const std::string &containerID) noexcept; void ServiceWorkThread(int threadid); void CloseWsSession(int socketID); void CloseAllWsSession(); diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c index 58303f80..00d130ac 100644 --- a/src/daemon/executor/container_cb/execution_extend.c +++ b/src/daemon/executor/container_cb/execution_extend.c @@ -69,7 +69,6 @@ static int service_events_handler(const struct isulad_events_request *request, c container_t *container = NULL; name = util_strdup_s(request->id); - /* check whether specified container exists */ if (name != NULL) { container = containers_store_get(name); diff --git a/src/daemon/modules/container/container_unix.c b/src/daemon/modules/container/container_unix.c index d9706f08..224be958 100644 --- a/src/daemon/modules/container/container_unix.c +++ b/src/daemon/modules/container/container_unix.c @@ -1073,15 +1073,13 @@ bool container_reset_restart_manager(container_t *cont, bool reset_count) /* get restart manager */ restart_manager_t *get_restart_manager(container_t *cont) { - int failue_count = 0; - if (cont == NULL) { ERROR("Invalid input arguments"); return NULL; } if (cont->rm == NULL) { - failue_count = container_state_get_restart_count(cont->state); + int failue_count = container_state_get_restart_count(cont->state); cont->rm = restart_manager_new(cont->hostconfig->restart_policy, failue_count); if (cont->rm == NULL) { return NULL; diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c index 01b9ef6e..d1fbeafb 100644 --- a/src/daemon/modules/image/oci/oci_load.c +++ b/src/daemon/modules/image/oci/oci_load.c @@ -1,5 +1,5 @@ /****************************************************************************** -* Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved. +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. * iSulad licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c index 97919603..e8bc32ea 100644 --- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c +++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c @@ -96,6 +96,7 @@ static int handle_dm_thinpooldev(char *val, struct device_set *devset) return -1; } tmp_val = util_trim_prefice_string(val, "/dev/mapper/"); + free(devset->thin_pool_device); devset->thin_pool_device = util_strdup_s(tmp_val); return 0; @@ -160,6 +161,7 @@ static int handle_dm_mountopt(char *val, struct device_set *devset) isulad_set_error_message("Invalid dm.mountopt or devicemapper.mountopt value"); return -1; } + free(devset->mount_options); devset->mount_options = util_strdup_s(val); return 0; diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c index 7e851a26..c5864c90 100644 --- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c +++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved. + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. * iSulad licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -1948,7 +1948,6 @@ static int get_lower_dirs(const char *layer_dir, const struct graphdriver *drive lowers_str = read_layer_lower_file(layer_dir); lowers = util_string_split(lowers_str, ':'); lowers_size = util_array_len((const char **)lowers); - if (lowers_size == 0) { ret = 0; goto out; diff --git a/src/utils/cpputils/url.cc b/src/utils/cpputils/url.cc index c75a17aa..49843644 100644 --- a/src/utils/cpputils/url.cc +++ b/src/utils/cpputils/url.cc @@ -105,8 +105,10 @@ std::string QueryUnescape(const std::string &s) int UnescapeDealWithPercentSign(size_t &i, std::string &s, const EncodeMode &mode) { std::string percent_sign = "%25"; + // URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits if ((size_t)(i + 2) >= s.length() || !IsHex(s[i + 1]) || !IsHex(s[i + 2])) { s.erase(s.begin(), s.begin() + (long)i); + // Remaining "%" with two hexadecimal digits if (s.length() > 3) { s.erase(s.begin() + 3, s.end()); } @@ -172,6 +174,7 @@ void DoUnescape(std::string &t, const std::string &s, const EncodeMode &mode) switch (s[i]) { case '%': { char s1, s2; + // the "%" is followed by two hexadecimal digits if (!GetHexDigit(s[i + 1], s1) || !GetHexDigit(s[i + 2], s2)) { return; } diff --git a/src/utils/cpputils/url.h b/src/utils/cpputils/url.h index 9b124c79..abbf20f4 100644 --- a/src/utils/cpputils/url.h +++ b/src/utils/cpputils/url.h @@ -183,5 +183,3 @@ bool ValidUserinfo(const std::string &s); } // namespace url #endif - - diff --git a/src/utils/cutils/utils_mount_spec.c b/src/utils/cutils/utils_mount_spec.c index 5386c115..e267c5a2 100644 --- a/src/utils/cutils/utils_mount_spec.c +++ b/src/utils/cutils/utils_mount_spec.c @@ -1,5 +1,5 @@ /****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved. + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. * iSulad licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: diff --git a/src/utils/cutils/utils_timestamp.c b/src/utils/cutils/utils_timestamp.c index 7435e2fa..fee66ea8 100644 --- a/src/utils/cutils/utils_timestamp.c +++ b/src/utils/cutils/utils_timestamp.c @@ -155,6 +155,7 @@ static bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timeb int32_t nanos; struct tm tm_local = { 0 }; size_t tmp_size = 0; + size_t timebuffer_len = 0; time_t seconds; bool west_timezone = false; long int tm_gmtoff = 0; @@ -167,7 +168,11 @@ static bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timeb seconds = (time_t)timestamp->seconds; localtime_r(&seconds, &tm_local); - strftime(timebuffer, maxsize, "%Y-%m-%dT%H:%M:%S", &tm_local); + timebuffer_len = strftime(timebuffer, maxsize, "%Y-%m-%dT%H:%M:%S", &tm_local); + if (timebuffer_len == 0) { + ERROR("Failed to strftime"); + return false; + } if (timestamp->has_nanos) { nanos = timestamp->nanos; @@ -175,10 +180,10 @@ static bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timeb nanos = 0; } - tmp_size = maxsize - strlen(timebuffer); + tmp_size = maxsize - timebuffer_len; if (local_utc) { - nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09dZ", nanos); + nret = snprintf(timebuffer + timebuffer_len, tmp_size, ".%09dZ", nanos); goto out; } @@ -197,9 +202,9 @@ static bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timeb tm_zone_min = (tm_gmtoff - tm_zone_hour * seconds_per_hour) / seconds_per_minutes; if (!west_timezone) { - nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09d+%02d:%02d", nanos, tm_zone_hour, tm_zone_min); + nret = snprintf(timebuffer + timebuffer_len, tmp_size, ".%09d+%02d:%02d", nanos, tm_zone_hour, tm_zone_min); } else { - nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09d-%02d:%02d", nanos, tm_zone_hour, tm_zone_min); + nret = snprintf(timebuffer + timebuffer_len, tmp_size, ".%09d-%02d:%02d", nanos, tm_zone_hour, tm_zone_min); } out: diff --git a/src/utils/http/http.c b/src/utils/http/http.c index 47d17455..1b0ba5be 100644 --- a/src/utils/http/http.c +++ b/src/utils/http/http.c @@ -362,7 +362,8 @@ static size_t calc_replaced_url_len(const char *url) max++; continue; } - max += 3; /* ' ' to %20 so size should add 3 */ + /* ' ' to %20 so size should add 3 */ + max += 3; } return max + 1; /* +1 for terminator */ @@ -486,7 +487,6 @@ int http_request(const char *url, struct http_get_options *options, long *respon /* get it! */ curl_result = curl_easy_perform(curl_handle); - if (curl_result != CURLE_OK) { check_buf_len(options, errbuf, curl_result); ret = -1; diff --git a/src/utils/http/parser.c b/src/utils/http/parser.c index d625182c..cf8425e4 100644 --- a/src/utils/http/parser.c +++ b/src/utils/http/parser.c @@ -47,15 +47,15 @@ #include "utils.h" #include "isula_libutils/log.h" -size_t strlncat(char *dststr, size_t size, const char *srcstr, size_t nsize) +size_t strlncat(char *dststr, size_t dststr_size, const char *srcstr, size_t srcstr_size) { size_t ssize, dsize; - ssize = (size_t)strnlen(srcstr, nsize); - dsize = (size_t)strnlen(dststr, size); + ssize = (size_t)strnlen(srcstr, srcstr_size); + dsize = (size_t)strnlen(dststr, dststr_size); - if (dsize < size) { - size_t rsize = size - dsize; + if (dsize < dststr_size) { + size_t rsize = dststr_size - dsize; size_t ncpy = ssize < rsize ? ssize : (rsize - 1); (void)memcpy(dststr + dsize, srcstr, ncpy); dststr[dsize + ncpy] = '\0'; diff --git a/src/utils/sha256/sha256.c b/src/utils/sha256/sha256.c index 81375111..674c3289 100644 --- a/src/utils/sha256/sha256.c +++ b/src/utils/sha256/sha256.c @@ -173,18 +173,16 @@ char *sha256_digest_file(const char *filename, bool isgzip) break; } - if (n > 0) { #if OPENSSL_VERSION_MAJOR >= 3 - if (!EVP_DigestUpdate(ctx, (unsigned char *)buffer, n)) { - ERROR("Failed to pass the message to be digested"); - ERR_print_errors_fp(stderr); - ret = -1; - goto out; - } + if (!EVP_DigestUpdate(ctx, (unsigned char *)buffer, n)) { + ERROR("Failed to pass the message to be digested"); + ERR_print_errors_fp(stderr); + ret = -1; + goto out; + } #else - SHA256_Update(&ctx, buffer, n); + SHA256_Update(&ctx, buffer, n); #endif - } if (stream_check_eof(stream, isgzip)) { break; -- 2.40.1