From b01d0518d63cca451e2f0e88dc0d1cee2474fa7b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 28 Aug 2023 20:10:13 +0800
Subject: [PATCH 123/145] improve by code check of cpp

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 src/daemon/entry/cri/errors.cc        |  1 +
 src/daemon/entry/cri/errors.h         |  2 +-
 src/utils/cpputils/cxxutils.cc        | 10 ++++----
 src/utils/cpputils/read_write_lock.h  |  2 --
 src/utils/cpputils/stoppable_thread.h |  1 -
 src/utils/cpputils/url.cc             | 37 +++++++++++++++------------
 src/utils/cutils/utils_file.h         |  2 --
 src/utils/cutils/utils_timestamp.h    |  2 --
 src/utils/http/rest_common.h          |  2 --
 src/utils/tar/isulad_tar.c            |  3 ++-
 src/utils/tar/isulad_tar.h            |  2 --
 src/utils/tar/util_archive.c          |  9 ++++---
 src/utils/tar/util_archive.h          |  2 --
 13 files changed, 35 insertions(+), 40 deletions(-)

diff --git a/src/daemon/entry/cri/errors.cc b/src/daemon/entry/cri/errors.cc
index 3dc3bba6..e0fdc7f7 100644
--- a/src/daemon/entry/cri/errors.cc
+++ b/src/daemon/entry/cri/errors.cc
@@ -50,6 +50,7 @@ std::string &Errors::GetMessage()
     return m_message;
 }
 
+// never save return pointer, we just use for printf of logging
 const char *Errors::GetCMessage() const
 {
     return m_message.empty() ? "" : m_message.c_str();
diff --git a/src/daemon/entry/cri/errors.h b/src/daemon/entry/cri/errors.h
index 193a9523..33be6d77 100644
--- a/src/daemon/entry/cri/errors.h
+++ b/src/daemon/entry/cri/errors.h
@@ -25,7 +25,7 @@ public:
         : m_message(copy.m_message), m_code(copy.m_code)
     {
     }
-    Errors &operator=(const Errors &);
+    Errors &operator=(const Errors &other);
     virtual ~Errors();
 
     void Clear();
diff --git a/src/utils/cpputils/cxxutils.cc b/src/utils/cpputils/cxxutils.cc
index 777c52e7..b4fb3c3f 100644
--- a/src/utils/cpputils/cxxutils.cc
+++ b/src/utils/cpputils/cxxutils.cc
@@ -19,13 +19,13 @@
 namespace CXXUtils {
 std::vector<std::string> Split(const std::string &str, char delimiter)
 {
-    std::vector<std::string> ret_vec;
-    std::string tmpstr;
+    std::vector<std::string> retVec;
+    std::string tmpStr;
     std::istringstream istream(str);
-    while (std::getline(istream, tmpstr, delimiter)) {
-        ret_vec.push_back(tmpstr);
+    while (std::getline(istream, tmpStr, delimiter)) {
+        retVec.push_back(tmpStr);
     }
-    return ret_vec;
+    return retVec;
 }
 
 // Join concatenates the elements of a to create a single string. The separator string
diff --git a/src/utils/cpputils/read_write_lock.h b/src/utils/cpputils/read_write_lock.h
index 0149e3a5..047d459f 100644
--- a/src/utils/cpputils/read_write_lock.h
+++ b/src/utils/cpputils/read_write_lock.h
@@ -15,10 +15,8 @@
 #ifndef UTILS_CPPUTILS_READ_WRITE_LOCK_H
 #define UTILS_CPPUTILS_READ_WRITE_LOCK_H
 
-#include <iostream>
 #include <mutex>
 #include <condition_variable>
-#include <thread>
 
 class RWMutex {
 public:
diff --git a/src/utils/cpputils/stoppable_thread.h b/src/utils/cpputils/stoppable_thread.h
index f5f4fb3f..dada6b50 100644
--- a/src/utils/cpputils/stoppable_thread.h
+++ b/src/utils/cpputils/stoppable_thread.h
@@ -16,7 +16,6 @@
 #define UTILS_CPPUTILS_STOPPABLE_THREAD_H
 
 #include <iostream>
-#include <chrono>
 #include <future>
 #include <mutex>
 #include <utility>
diff --git a/src/utils/cpputils/url.cc b/src/utils/cpputils/url.cc
index 49843644..03ff2653 100644
--- a/src/utils/cpputils/url.cc
+++ b/src/utils/cpputils/url.cc
@@ -31,6 +31,7 @@ bool GetHexDigit(char c, char &d)
     if (c >= '0' && c <= '9') {
         d = c - '0';
     } else if (c >= 'a' && c <= 'f') {
+        // 10 equal to a in hex
         d = c - 'a' + 10;
     } else {
         d = c - 'A' + 10;
@@ -104,7 +105,7 @@ std::string QueryUnescape(const std::string &s)
 
 int UnescapeDealWithPercentSign(size_t &i, std::string &s, const EncodeMode &mode)
 {
-    std::string percent_sign = "%25";
+    std::string percentSign = "%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);
@@ -119,15 +120,16 @@ int UnescapeDealWithPercentSign(size_t &i, std::string &s, const EncodeMode &mod
     if (!GetHexDigit(s[i + 1], s1) || !GetHexDigit(s[i + 2], s2)) {
         return -1;
     }
+    // for 3 bit hex, max value is 8
     if (mode == EncodeMode::ENCODE_HOST && s1 < 8 &&
-        std::string(s.begin() + (long)i, s.begin() + (long)i + 3) != percent_sign) {
-        ERROR("invalid URL escape %s", std::string(s.begin() + (long)i, s.begin() + (long)i + 3).c_str());
+        std::string(s.begin() + static_cast<long>(i), s.begin() + static_cast<long>(i+3)) != percentSign) {
+        ERROR("invalid URL escape %s", std::string(s.begin() + static_cast<long>(i), s.begin() + static_cast<long>(i + 3)).c_str());
         return -1;
     }
     if (mode == EncodeMode::ENCODE_ZONE) {
         char v = static_cast<char>((static_cast<unsigned char>(s1) << 4) | static_cast<unsigned char>(s2));
-        if (std::string(s.begin() + static_cast<long>(i), s.begin() + static_cast<long>(i) + 3) != percent_sign && v != ' ' &&
-            ShouldEscape(v, EncodeMode::ENCODE_HOST)) {
+        if (std::string(s.begin() + static_cast<long>(i), s.begin() + static_cast<long>(i) + 3) != percentSign &&
+            v != ' ' && ShouldEscape(v, EncodeMode::ENCODE_HOST)) {
             ERROR("invalid URL escape %s",
                   std::string(s.begin() + static_cast<long>(i), s.begin() + static_cast<long>(i) + 3).c_str());
             return -1;
@@ -178,7 +180,8 @@ void DoUnescape(std::string &t, const std::string &s, const EncodeMode &mode)
                     if (!GetHexDigit(s[i + 1], s1) || !GetHexDigit(s[i + 2], s2)) {
                         return;
                     }
-                    t[j++] = (char)(((unsigned char)s1 << 4) | (unsigned char)s2);
+                    // we use 4 high bit of 1 + 4 low bit of s2 to create new char
+                    t[j++] = (char)((static_cast<unsigned char>(s1) << 4) | static_cast<unsigned char>(s2));
                     i += 3;
                 }
                 break;
@@ -249,7 +252,9 @@ std::string Escape(const std::string &s, const EncodeMode &mode)
             t[j++] = '+';
         } else if (ShouldEscape(c, mode)) {
             t[j] = '%';
-            t[j + 1] = "0123456789ABCDEF"[(unsigned char)c >> 4];
+            // get 4 high bit of c
+            t[j + 1] = "0123456789ABCDEF"[static_cast<unsigned char>(c) >> 4];
+            // get 4 low bit of c
             t[j + 2] = "0123456789ABCDEF"[c & 15];
             j += 3;
         } else {
@@ -288,8 +293,8 @@ int Getscheme(const std::string &rawurl, std::string &scheme, std::string &path)
                 ERROR("missing protocol scheme");
                 return -1;
             }
-            scheme = std::string(rawurl.begin(), rawurl.begin() + (long)i);
-            path = std::string(rawurl.begin() + (long)i + 1, rawurl.end());
+            scheme = std::string(rawurl.begin(), rawurl.begin() + static_cast<long>(i));
+            path = std::string(rawurl.begin() + static_cast<long>(i + 1), rawurl.end());
             return 0;
         } else {
             scheme = "";
@@ -355,25 +360,25 @@ int SplitOffPossibleLeading(std::string &scheme, const std::string &rawurl, URLD
 }
 
 URLDatum *HandleNonBackslashPrefix(URLDatum *url, const std::string &scheme, const std::string &rest, bool viaRequest,
-                                   bool &should_ret)
+                                   bool &shouldRet)
 {
     if (rest.at(0) == '/') {
         return nullptr;
     }
     if (!scheme.empty()) {
-        should_ret = true;
+        shouldRet = true;
         url->SetOpaque(rest);
         return url;
     }
     if (viaRequest) {
-        should_ret = true;
+        shouldRet = true;
         ERROR("invalid URI for request");
         return nullptr;
     }
     size_t colon = rest.find(":");
     size_t slash = rest.find("/");
     if (colon != std::string::npos && (slash == std::string::npos || colon < slash)) {
-        should_ret = true;
+        shouldRet = true;
         ERROR("first path segment in URL cannot contain colon");
         return nullptr;
     }
@@ -420,9 +425,9 @@ URLDatum *Parse(const std::string &rawurl, bool viaRequest)
     if (SplitOffPossibleLeading(scheme, rawurl, url, rest) != 0) {
         return nullptr;
     }
-    bool should_ret = false;
-    auto *tmpret = HandleNonBackslashPrefix(url, scheme, rest, viaRequest, should_ret);
-    if (should_ret) {
+    bool shouldRet = false;
+    auto *tmpret = HandleNonBackslashPrefix(url, scheme, rest, viaRequest, shouldRet);
+    if (shouldRet) {
         return tmpret;
     }
     if (SetURLDatumInfo(url, scheme, viaRequest, rest) != 0) {
diff --git a/src/utils/cutils/utils_file.h b/src/utils/cutils/utils_file.h
index 9bf60c6c..aca066ab 100644
--- a/src/utils/cutils/utils_file.h
+++ b/src/utils/cutils/utils_file.h
@@ -22,8 +22,6 @@
 #include <stdio.h>
 #include <sys/types.h>
 
-struct dirent;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/utils/cutils/utils_timestamp.h b/src/utils/cutils/utils_timestamp.h
index 2e93a215..4bfe796d 100644
--- a/src/utils/cutils/utils_timestamp.h
+++ b/src/utils/cutils/utils_timestamp.h
@@ -20,8 +20,6 @@
 #include <sys/types.h>
 #include <time.h>
 
-struct tm;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/utils/http/rest_common.h b/src/utils/http/rest_common.h
index aebc48c0..6363e67d 100644
--- a/src/utils/http/rest_common.h
+++ b/src/utils/http/rest_common.h
@@ -21,8 +21,6 @@
 #include "http/http.h"
 #include "parser.h"
 
-struct parsed_http_message;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/utils/tar/isulad_tar.c b/src/utils/tar/isulad_tar.c
index 48ac96da..709dfdd9 100644
--- a/src/utils/tar/isulad_tar.c
+++ b/src/utils/tar/isulad_tar.c
@@ -23,10 +23,11 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#include <isula_libutils/log.h>
+
 #include "stdbool.h"
 #include "utils.h"
 #include "path.h"
-#include "isula_libutils/log.h"
 #include "error.h"
 #include "isula_libutils/json_common.h"
 #include "util_archive.h"
diff --git a/src/utils/tar/isulad_tar.h b/src/utils/tar/isulad_tar.h
index ec085c25..b620fd02 100644
--- a/src/utils/tar/isulad_tar.h
+++ b/src/utils/tar/isulad_tar.h
@@ -25,8 +25,6 @@
 
 #include "io_wrapper.h"
 
-struct io_read_wrapper;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/utils/tar/util_archive.c b/src/utils/tar/util_archive.c
index 19e6a6b1..a9d0025b 100644
--- a/src/utils/tar/util_archive.c
+++ b/src/utils/tar/util_archive.c
@@ -21,20 +21,21 @@
 #include <sys/types.h>
 #include <sys/xattr.h>
 #include <sys/stat.h>
+#include <sys/mount.h>
+#include <sys/capability.h>
 #include <archive.h>
 #include <archive_entry.h>
 #include <errno.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <libgen.h>
 #include <pwd.h>
 #include <netdb.h>
-#include <sys/mount.h>
-#include <sys/capability.h>
 
-#include "stdbool.h"
+#include <isula_libutils/log.h>
+
 #include "utils.h"
-#include "isula_libutils/log.h"
 #include "io_wrapper.h"
 #include "utils_file.h"
 #include "map.h"
diff --git a/src/utils/tar/util_archive.h b/src/utils/tar/util_archive.h
index 30f35e19..9312235d 100644
--- a/src/utils/tar/util_archive.h
+++ b/src/utils/tar/util_archive.h
@@ -26,8 +26,6 @@
 
 #define ARCHIVE_BLOCK_SIZE (32 * 1024)
 
-struct io_read_wrapper;
-
 #ifdef __cplusplus
 extern "C" {
 #endif
-- 
2.40.1

