From dff4e8fc306e2270695c20434f79bcf3303c8f37 Mon Sep 17 00:00:00 2001 From: wujing Date: Tue, 18 Jan 2022 17:21:15 +0800 Subject: [PATCH 09/16] fix utils module encoding problem Signed-off-by: wujing --- src/utils/console/console.c | 2 +- src/utils/console/console.h | 4 +- src/utils/cpputils/stoppable_thread.cc | 7 +-- src/utils/cpputils/stoppable_thread.h | 15 ++++--- src/utils/cpputils/url.cc | 61 ++++++++++++-------------- src/utils/cutils/utils_aes.c | 4 +- src/utils/cutils/utils_mount_spec.c | 41 ++++++++--------- src/utils/cutils/utils_string.c | 4 +- src/utils/cutils/utils_timestamp.c | 11 +++-- 9 files changed, 73 insertions(+), 76 deletions(-) diff --git a/src/utils/console/console.c b/src/utils/console/console.c index b1d8b6dc..17c8b242 100644 --- a/src/utils/console/console.c +++ b/src/utils/console/console.c @@ -437,7 +437,7 @@ err_out: /* console loop copy */ int console_loop_io_copy(int sync_fd, const int *srcfds, struct io_write_wrapper *writers, - transfer_channel_type *channels, size_t len) + const transfer_channel_type *channels, size_t len) { int ret = 0; size_t i = 0; diff --git a/src/utils/console/console.h b/src/utils/console/console.h index 63103d2b..f42ec994 100644 --- a/src/utils/console/console.h +++ b/src/utils/console/console.h @@ -43,7 +43,7 @@ struct tty_state { bool ignore_stdin_close; }; -typedef enum { STDIN_CHANNEL, STDOUT_CHANNEL, STDERR_CHANNEL, MAX_CHANNEL} transfer_channel_type; +typedef enum { STDIN_CHANNEL, STDOUT_CHANNEL, STDERR_CHANNEL, MAX_CHANNEL } transfer_channel_type; int console_fifo_name(const char *rundir, const char *subpath, const char *stdflag, char *fifo_name, size_t fifo_name_sz, char *fifo_path, size_t fifo_path_sz, bool do_mkdirp); @@ -62,7 +62,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin int tty_exit, bool tty); int console_loop_io_copy(int sync_fd, const int *srcfds, struct io_write_wrapper *writers, - transfer_channel_type *channels, size_t len); + const transfer_channel_type *channels, size_t len); int setup_tios(int fd, struct termios *curr_tios); diff --git a/src/utils/cpputils/stoppable_thread.cc b/src/utils/cpputils/stoppable_thread.cc index 68f6d9b2..20c6b374 100644 --- a/src/utils/cpputils/stoppable_thread.cc +++ b/src/utils/cpputils/stoppable_thread.cc @@ -15,7 +15,7 @@ #include "stoppable_thread.h" -StoppableThread &StoppableThread::operator=(StoppableThread &&obj) +StoppableThread &StoppableThread::operator=(StoppableThread &&obj) noexcept { m_exit_signal = std::move(obj.m_exit_signal); m_future_obj = std::move(obj.m_future_obj); @@ -24,10 +24,7 @@ StoppableThread &StoppableThread::operator=(StoppableThread &&obj) bool StoppableThread::stopRequested() { - if (m_future_obj.wait_for(std::chrono::milliseconds(0)) == std::future_status::timeout) { - return false; - } - return true; + return m_future_obj.wait_for(std::chrono::milliseconds(0)) != std::future_status::timeout; } void StoppableThread::stop() diff --git a/src/utils/cpputils/stoppable_thread.h b/src/utils/cpputils/stoppable_thread.h index 7dfd61f9..f5f4fb3f 100644 --- a/src/utils/cpputils/stoppable_thread.h +++ b/src/utils/cpputils/stoppable_thread.h @@ -23,12 +23,18 @@ class StoppableThread { public: - StoppableThread() : m_future_obj(m_exit_signal.get_future()) {} + StoppableThread() + : m_future_obj(m_exit_signal.get_future()) + { + } - explicit StoppableThread(StoppableThread &&obj) : m_exit_signal(std::move(obj.m_exit_signal)), - m_future_obj(std::move(obj.m_future_obj)) {} + explicit StoppableThread(StoppableThread &&obj) noexcept + : m_exit_signal(std::move(obj.m_exit_signal)) + , m_future_obj(std::move(obj.m_future_obj)) + { + } - StoppableThread &operator=(StoppableThread &&obj); + StoppableThread &operator=(StoppableThread &&obj) noexcept; virtual ~StoppableThread() = default; @@ -49,4 +55,3 @@ private: }; #endif // UTILS_CPPUTILS_STOPPABLE_THREAD_H - diff --git a/src/utils/cpputils/url.cc b/src/utils/cpputils/url.cc index 39032feb..ab1355a3 100644 --- a/src/utils/cpputils/url.cc +++ b/src/utils/cpputils/url.cc @@ -109,7 +109,7 @@ int UnescapeDealWithPercentSign(size_t &i, std::string &s, const EncodeMode &mod if (s.length() > 3) { s.erase(s.begin() + 3, s.end()); } - ERROR("invalid URL escape %s", s.c_str()); // quoted + ERROR("invalid URL escape %s", s.c_str()); // quoted return -1; } char s1, s2; @@ -123,10 +123,10 @@ int UnescapeDealWithPercentSign(size_t &i, std::string &s, const EncodeMode &mod } if (mode == EncodeMode::ENCODE_ZONE) { char v = static_cast((static_cast(s1) << 4) | static_cast(s2)); - if (std::string(s.begin() + static_cast(i), s.begin() + static_cast(i) + 3) != "%25" && - v != ' ' && ShouldEscape(v, EncodeMode::ENCODE_HOST)) { - ERROR("invalid URL escape %s", std::string(s.begin() + static_cast(i), - s.begin() + static_cast(i) + 3).c_str()); + if (std::string(s.begin() + static_cast(i), s.begin() + static_cast(i) + 3) != "%25" && v != ' ' && + ShouldEscape(v, EncodeMode::ENCODE_HOST)) { + ERROR("invalid URL escape %s", + std::string(s.begin() + static_cast(i), s.begin() + static_cast(i) + 3).c_str()); return -1; } } @@ -153,8 +153,8 @@ int CalculatePercentNum(std::string &s, const EncodeMode &mode, bool &hasPlus) } break; default: - if ((mode == EncodeMode::ENCODE_HOST || mode == EncodeMode::ENCODE_ZONE) && - int(s[i]) < 0x80 && ShouldEscape(s[i], mode)) { + if ((mode == EncodeMode::ENCODE_HOST || mode == EncodeMode::ENCODE_ZONE) && int(s[i]) < 0x80 && + ShouldEscape(s[i], mode)) { ERROR("invalid URL escape %s", std::string(s.begin() + (long)i, s.begin() + (long)i + 1).c_str()); return -1; } @@ -319,7 +319,7 @@ URLDatum *Parse(const std::string &rawurl) { std::string u, frag; Split(rawurl, "#", true, u, frag); - auto url = Parse(u, false); + auto *url = Parse(u, false); if (url == nullptr) { return nullptr; } @@ -335,12 +335,11 @@ URLDatum *Parse(const std::string &rawurl) int SplitOffPossibleLeading(std::string &scheme, const std::string &rawurl, URLDatum *url, std::string &rest) { - if (Getscheme(rawurl, scheme, rest)) { + if (Getscheme(rawurl, scheme, rest) != 0) { return -1; } std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower); - if (rest.at(rest.length() - 1) == '?' && - std::count(rest.begin(), rest.end(), '?') == 1) { + if (rest.at(rest.length() - 1) == '?' && std::count(rest.begin(), rest.end(), '?') == 1) { url->SetForceQuery(true); rest = rest.substr(0, rest.length() - 1); } else { @@ -351,8 +350,8 @@ int SplitOffPossibleLeading(std::string &scheme, const std::string &rawurl, URLD return 0; } -URLDatum *HandleNonBackslashPrefix(URLDatum *url, const std::string &scheme, - const std::string &rest, bool viaRequest, bool &should_ret) +URLDatum *HandleNonBackslashPrefix(URLDatum *url, const std::string &scheme, const std::string &rest, bool viaRequest, + bool &should_ret) { if (rest.at(0) == '/') { return nullptr; @@ -403,7 +402,7 @@ URLDatum *Parse(const std::string &rawurl, bool viaRequest) ERROR("empty url!"); return nullptr; } - URLDatum *url = new (std::nothrow) URLDatum; + auto *url = new (std::nothrow) URLDatum; if (url == nullptr) { ERROR("Out of memory"); return nullptr; @@ -414,15 +413,15 @@ URLDatum *Parse(const std::string &rawurl, bool viaRequest) } std::string scheme = url->GetScheme(); std::string rest; - if (SplitOffPossibleLeading(scheme, rawurl, url, rest)) { + if (SplitOffPossibleLeading(scheme, rawurl, url, rest) != 0) { return nullptr; } bool should_ret = false; - auto tmpret = HandleNonBackslashPrefix(url, scheme, rest, viaRequest, should_ret); + auto *tmpret = HandleNonBackslashPrefix(url, scheme, rest, viaRequest, should_ret); if (should_ret) { return tmpret; } - if (SetURLDatumInfo(url, scheme, viaRequest, rest)) { + if (SetURLDatumInfo(url, scheme, viaRequest, rest) != 0) { return nullptr; } return url; @@ -432,13 +431,13 @@ int ParseAuthority(const std::string &authority, UserInfo **user, std::string &h { size_t i = authority.find("@"); if (i == std::string::npos) { - if (ParseHost(authority, host)) { + if (ParseHost(authority, host) != 0) { *user = nullptr; host = ""; return -1; } } else { - if (ParseHost(authority.substr(i + 1, authority.size()), host)) { + if (ParseHost(authority.substr(i + 1, authority.size()), host) != 0) { *user = nullptr; host = ""; return -1; @@ -602,7 +601,7 @@ std::string GetFullPreResolvePath(const std::string &base, const std::string &re void SplitFullPreResolvePath(const std::string &full, std::vector &dst) { std::vector src = CXXUtils::Split(full, '/'); - for (auto elem : src) { + for (const auto &elem : src) { if (elem == ".") { continue; } else if (elem == "..") { @@ -676,8 +675,8 @@ bool ValidUserinfo(const std::string &s) { std::string subDelims = R"(-._:~!$&'()*+,;=%@)"; for (const auto &r : s) { - if (('A' <= r && r <= 'Z') || ('a' <= r && r <= 'z') || - ('0' <= r && r <= '9') || (subDelims.find(r) != std::string::npos)) { + if (('A' <= r && r <= 'Z') || ('a' <= r && r <= 'z') || ('0' <= r && r <= '9') || + (subDelims.find(r) != std::string::npos)) { continue; } return false; @@ -687,11 +686,11 @@ bool ValidUserinfo(const std::string &s) std::string Values::Get(const std::string &key) { - if (v.size() == 0) { + if (v.empty()) { return ""; } std::vector vs = v[key]; - if (vs.size() == 0) { + if (vs.empty()) { return ""; } return vs[0]; @@ -731,7 +730,7 @@ std::string Values::Encode() for (auto k : keys) { std::vector vs = v[k]; std::string keyEscaped = QueryEscape(k); - for (auto elem : vs) { + for (const auto &elem : vs) { if (buf.length() > 0) { buf.append("&"); } @@ -817,8 +816,7 @@ void URLDatum::StringOpaqueEmptyRules(std::string &buf) } if (buf.length() == 0) { auto i = m_path.find(":"); - if (i != std::string::npos && - path.substr(0, i).find("/") == std::string::npos) { + if (i != std::string::npos && path.substr(0, i).find("/") == std::string::npos) { buf.append("./"); } } @@ -855,7 +853,7 @@ bool URLDatum::IsAbs() const std::unique_ptr URLDatum::UrlParse(const std::string &ref) { - auto refurl = Parse(ref); + auto *refurl = Parse(ref); if (refurl == nullptr) { return nullptr; } @@ -894,8 +892,7 @@ std::unique_ptr URLDatum::ResolveReference(URLDatum *ref) return url; } - -auto URLDatum::Query() ->std::map> +auto URLDatum::Query() -> std::map> { return ParseQuery(m_rawQuery); } @@ -928,6 +925,4 @@ std::string URLDatum::Port() const { return PortOnly(m_host); } -} // namespace url - - +} // namespace url diff --git a/src/utils/cutils/utils_aes.c b/src/utils/cutils/utils_aes.c index 1df95fd5..5dc822a2 100644 --- a/src/utils/cutils/utils_aes.c +++ b/src/utils/cutils/utils_aes.c @@ -95,9 +95,9 @@ size_t util_aes_decode_buf_len(size_t len) { if (len % AES_BLOCK_SIZE == 0) { return len; - } else { - return (len / AES_BLOCK_SIZE * AES_BLOCK_SIZE) + AES_BLOCK_SIZE; } + + return (len / AES_BLOCK_SIZE * AES_BLOCK_SIZE) + AES_BLOCK_SIZE; } size_t util_aes_encode_buf_len(size_t len) diff --git a/src/utils/cutils/utils_mount_spec.c b/src/utils/cutils/utils_mount_spec.c index a262a249..d8f64c81 100644 --- a/src/utils/cutils/utils_mount_spec.c +++ b/src/utils/cutils/utils_mount_spec.c @@ -31,8 +31,8 @@ #include "path.h" #define CACHE_ERRMSG_LEN 512 -#define CACHE_ERRMSG(errmsg, fmt, args...) \ - do { \ +#define CACHE_ERRMSG(errmsg, fmt, args...) \ + do { \ (void)snprintf(errmsg, CACHE_ERRMSG_LEN, fmt, ##args); \ } while (0) @@ -49,8 +49,8 @@ static int parse_mount_item_type(const char *value, char *mount_str, mount_spec } #ifdef ENABLE_OCI_IMAGE - if (strcmp(value, MOUNT_TYPE_SQUASHFS) && strcmp(value, MOUNT_TYPE_BIND) && - strcmp(value, MOUNT_TYPE_VOLUME) && strcmp(value, MOUNT_TYPE_TMPFS)) { + if (strcmp(value, MOUNT_TYPE_SQUASHFS) != 0 && strcmp(value, MOUNT_TYPE_BIND) != 0 && + strcmp(value, MOUNT_TYPE_VOLUME) != 0 && strcmp(value, MOUNT_TYPE_TMPFS) != 0) { CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Type must be one of squashfs/bind/volume/tmpfs", mount_str); #else @@ -67,7 +67,7 @@ static int parse_mount_item_type(const char *value, char *mount_str, mount_spec static int parse_mount_item_src(const char *value, char *mount_str, mount_spec *m, char *errmsg) { - char srcpath[PATH_MAX] = {0}; + char srcpath[PATH_MAX] = { 0 }; /* If value of source is NULL, ignore it */ if (value == NULL) { @@ -88,7 +88,8 @@ static int parse_mount_item_src(const char *value, char *mount_str, mount_spec * if (value[0] == '/') { if (!util_clean_path(value, srcpath, sizeof(srcpath))) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Can't translate source path to clean path", mount_str); + CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Can't translate source path to clean path", + mount_str); return EINVALIDARGS; } m->source = util_strdup_s(srcpath); @@ -119,7 +120,8 @@ static int parse_mount_item_dst(const char *value, char *mount_str, mount_spec * } if (!util_clean_path(value, dstpath, sizeof(dstpath))) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Can't translate destination path to clean path", mount_str); + CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Can't translate destination path to clean path", + mount_str); return EINVALIDARGS; } @@ -306,7 +308,7 @@ static int parse_mount_item_nocopy(const char *value, char *mount_str, mount_spe static bool exist_readonly_mode(char *mount_str) { - char tmp_mount_str[PATH_MAX] = {0}; + char tmp_mount_str[PATH_MAX] = { 0 }; int sret = 0; // add "," at start and end of mount string to simplize check @@ -332,25 +334,26 @@ static bool valid_mount_spec_mode(char *mount_str, mount_spec *m, char *errmsg) return false; } if (exist_readonly_mode(mount_str) && m->source == NULL) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Readonly mode must not be specified " - "for anonymous volume", mount_str); + CACHE_ERRMSG(errmsg, + "Invalid mount specification '%s'.Readonly mode must not be specified for anonymous volume", + mount_str); return false; } } if (strcmp(m->type, MOUNT_TYPE_BIND) == 0 && m->volume_options != NULL) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.nocopy must not be specified for type %s", - mount_str, m->type); + CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.nocopy must not be specified for type %s", mount_str, + m->type); return false; } if (strcmp(m->type, MOUNT_TYPE_TMPFS) == 0) { if (m->volume_options != NULL) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Cannot mix volume options with type %s", - mount_str, m->type); + CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Cannot mix volume options with type %s", mount_str, + m->type); return false; } if (m->bind_options != NULL) { - CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Cannot mix bind options with type %s", - mount_str, m->type); + CACHE_ERRMSG(errmsg, "Invalid mount specification '%s'.Cannot mix bind options with type %s", mount_str, + m->type); return false; } } @@ -415,7 +418,6 @@ static int check_mount_spec(char *mount_str, mount_spec *m, char *errmsg) return 0; } - static int parse_mounts_item(const char *mntkey, const char *value, char *mount_str, mount_spec *m, char *errmsg) { if (util_valid_key_type(mntkey)) { @@ -452,7 +454,7 @@ int util_parse_mount_spec(char *mount_str, mount_spec **spec, char **errmsg_out) size_t items_len = 0; char **items = NULL; char **key_val = NULL; - char errmsg[CACHE_ERRMSG_LEN] = {0}; + char errmsg[CACHE_ERRMSG_LEN] = { 0 }; if (mount_str == NULL) { CACHE_ERRMSG(errmsg, "Invalid mount specification: can't be empty"); @@ -528,7 +530,7 @@ bool util_valid_mount_spec(const char *mount_str, char **errmsg) mount_spec *m = NULL; // if parse success, it's valid - ret = util_parse_mount_spec((char*)mount_str, &m, errmsg); + ret = util_parse_mount_spec((char *)mount_str, &m, errmsg); if (ret != 0) { goto out; } @@ -538,4 +540,3 @@ out: return ret ? false : true; } - diff --git a/src/utils/cutils/utils_string.c b/src/utils/cutils/utils_string.c index 7e504326..8c9b2eea 100644 --- a/src/utils/cutils/utils_string.c +++ b/src/utils/cutils/utils_string.c @@ -795,7 +795,7 @@ bool util_has_prefix(const char *str, const char *prefix) return false; } - if (strncmp(str, prefix, strlen(prefix))) { + if (strncmp(str, prefix, strlen(prefix)) != 0) { return false; } @@ -817,7 +817,7 @@ bool util_has_suffix(const char *str, const char *suffix) return false; } - if (strcmp(str + str_len - suffix_len, suffix)) { + if (strcmp(str + str_len - suffix_len, suffix) != 0) { return false; } diff --git a/src/utils/cutils/utils_timestamp.c b/src/utils/cutils/utils_timestamp.c index 53ca7a95..ef695d3e 100644 --- a/src/utils/cutils/utils_timestamp.c +++ b/src/utils/cutils/utils_timestamp.c @@ -189,8 +189,7 @@ bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timebuffer, if (tm_zone >= 0) { nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09d+%02d:00", nanos, tm_zone); } else { - nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09d-%02d:00", nanos, - -tm_zone); + nret = snprintf(timebuffer + strlen(timebuffer), tmp_size, ".%09d-%02d:00", nanos, -tm_zone); } out: @@ -524,7 +523,7 @@ static char *tm_get_zp(const char *tmstr) return zp; } -static inline bool hasnil(const char *str, struct tm *tm, int32_t *nanos, struct types_timezone *tz) +static inline bool hasnil(const char *str, const struct tm *tm, const int32_t *nanos, const struct types_timezone *tz) { if (str == NULL || tm == NULL || nanos == NULL || tz == NULL) { return true; @@ -656,9 +655,9 @@ int64_t util_time_seconds_since(const char *in) if (result > 0) { return result; - } else { - return 0; } + + return 0; } struct time_human_duration_rule_t { @@ -1066,4 +1065,4 @@ int util_time_str_to_nanoseconds(const char *value, int64_t *nanoseconds) out: free(num_str); return ret; -} \ No newline at end of file +} -- 2.20.1