From cf1abd80174035c5561e7f64bdee7e99b9cab1ac Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Mon, 28 Aug 2023 06:46:10 +0000
Subject: [PATCH 119/145] !2135 modify incorrect variable type * modify
 incorrect variable type

---
 src/client/connect/protocol_type.h                |  2 +-
 src/daemon/modules/image/oci/oci_load.c           |  4 ++--
 src/daemon/modules/image/oci/registry/registry.c  | 12 +++++++-----
 .../oci/storage/layer_store/graphdriver/driver.c  |  2 +-
 .../image/oci/storage/layer_store/layer_store.c   | 15 +++++++++------
 src/daemon/modules/image/oci/storage/storage.c    |  2 +-
 src/daemon/modules/image/oci/utils_images.c       |  4 ++--
 src/daemon/modules/service/service_container.c    |  2 +-
 8 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/src/client/connect/protocol_type.h b/src/client/connect/protocol_type.h
index 814d32f0..47c5c6d1 100644
--- a/src/client/connect/protocol_type.h
+++ b/src/client/connect/protocol_type.h
@@ -239,7 +239,7 @@ typedef struct container_events_format {
     char *opt;
     char *id;
     char **annotations;
-    char annotations_len;
+    size_t annotations_len;
 } container_events_format_t;
 
 typedef void (*container_events_callback_t)(const container_events_format_t *event);
diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c
index d1fbeafb..fad69418 100644
--- a/src/daemon/modules/image/oci/oci_load.c
+++ b/src/daemon/modules/image/oci/oci_load.c
@@ -159,7 +159,7 @@ static void oci_load_free_layer(load_layer_blob_t *l)
 
 static void oci_load_free_image(load_image_t *im)
 {
-    int i = 0;
+    size_t i = 0;
 
     if (im == NULL) {
         return;
@@ -391,7 +391,7 @@ out:
 
 static int check_time_valid(oci_image_spec *conf)
 {
-    int i = 0;
+    size_t i = 0;
 
     if (!oci_valid_time(conf->created)) {
         ERROR("Invalid created time %s", conf->created);
diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c
index 402863a0..717f48b5 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -649,18 +649,20 @@ static int register_layer(pull_descriptor *desc, size_t i)
 
 static int get_top_layer_index(pull_descriptor *desc, size_t *top_layer_index)
 {
-    int i = 0;
+    size_t i;
 
     if (desc == NULL || top_layer_index == NULL) {
         ERROR("Invalid NULL pointer");
         return -1;
     }
-
-    for (i = desc->layers_len - 1; i >= 0; i--) {
-        if (desc->layers[i].empty_layer) {
+    // iterate over the layers array in reverse order, starting from the last layer
+    // since i is an unsigned number, i traverses from layers_len to 1
+    for (i = desc->layers_len; i > 0; i--) {
+        // the corresponding array index is [i - 1]: layers_len - 1 -> 0
+        if (desc->layers[i - 1].empty_layer) {
             continue;
         }
-        *top_layer_index = i;
+        *top_layer_index = i - 1;
         return 0;
     }
 
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/driver.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/driver.c
index b83c63b1..29223700 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/driver.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/driver.c
@@ -342,7 +342,7 @@ int graphdriver_apply_diff(const char *id, const struct io_read_wrapper *content
 container_inspect_graph_driver *graphdriver_get_metadata(const char *id)
 {
     int ret = -1;
-    int i = 0;
+    size_t i = 0;
     container_inspect_graph_driver *inspect_driver = NULL;
     json_map_string_string *metadata = NULL;
 
diff --git a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
index 4751f020..057ffaa3 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
@@ -752,7 +752,7 @@ out:
 static int insert_memory_stores(const char *id, const struct layer_opts *opts, layer_t *l)
 {
     int ret = 0;
-    int i = 0;
+    size_t i = 0;
 
     if (!append_layer_into_list(l)) {
         ret = -1;
@@ -793,9 +793,12 @@ clear_compress_digest:
         (void)delete_digest_from_map(g_metadata.by_compress_digest, l->slayer->compressed_diff_digest, id);
     }
 clear_by_name:
-    for (i = i - 1; i >= 0; i--) {
-        if (!map_remove(g_metadata.by_name, (void *)opts->names[i])) {
-            WARN("Remove name: %s failed", opts->names[i]);
+    // iterate over the names in reverse order, starting from the last name
+    // since i is an unsigned number, i traverses from inserted name len to 1
+    for (; i > 0; i--) {
+        // the corresponding array index is [i - 1]: inserted name len - 1 -> 0
+        if (!map_remove(g_metadata.by_name, (void *)opts->names[i - 1])) {
+            WARN("Remove name: %s failed", opts->names[i - 1]);
         }
     }
     if (!map_remove(g_metadata.by_id, (void *)id)) {
@@ -2032,7 +2035,7 @@ void layer_store_exit()
 static uint64_t payload_to_crc(char *payload)
 {
     int ret = 0;
-    int i = 0;
+    size_t i = 0;
     uint64_t crc = 0;
     uint8_t *crc_sums = NULL;
     size_t crc_sums_len = 0;
@@ -2452,7 +2455,7 @@ int remote_load_one_layer(const char *id)
 {
     int ret = 0;
     layer_t *tl = NULL;
-    int i = 0;
+    size_t i = 0;
 
     if (!layer_store_lock(true)) {
         return -1;
diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c
index 07c3830b..cece9e90 100644
--- a/src/daemon/modules/image/oci/storage/storage.c
+++ b/src/daemon/modules/image/oci/storage/storage.c
@@ -1536,7 +1536,7 @@ out:
 
 static bool is_rootfs_layer(const char *layer_id, const struct rootfs_list *all_rootfs)
 {
-    int j;
+    size_t j;
 
     if (all_rootfs == NULL || layer_id == NULL) {
         return false;
diff --git a/src/daemon/modules/image/oci/utils_images.c b/src/daemon/modules/image/oci/utils_images.c
index 6acbbb12..d06ba058 100644
--- a/src/daemon/modules/image/oci/utils_images.c
+++ b/src/daemon/modules/image/oci/utils_images.c
@@ -450,7 +450,7 @@ static char *convert_created_by(image_manifest_v1_compatibility *config)
 int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, const registry_manifest_schema1 *manifest,
                            docker_image_config_v2 *config)
 {
-    int i = 0;
+    size_t i = 0;
     int ret = 0;
     size_t history_index = 0;
     parser_error err = NULL;
@@ -511,7 +511,7 @@ int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, const re
 
         ret = util_array_append(&config->rootfs->diff_ids, layers[i].diff_id);
         if (ret != 0) {
-            ERROR("append diff id of layer %u to rootfs failed, diff id is %s", i, layers[i].diff_id);
+            ERROR("append diff id of layer %zu to rootfs failed, diff id is %s", i, layers[i].diff_id);
             ret = -1;
             goto out;
         }
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 43968c63..5d6cabf7 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -310,7 +310,6 @@ static int write_env_content(const char *env_path, const char **env, size_t env_
     int fd = -1;
     size_t i = 0;
     ssize_t nret = 0;
-    int env_max = 4096;
 
     ret = create_env_path_dir(env_path);
     if (ret < 0) {
@@ -325,6 +324,7 @@ static int write_env_content(const char *env_path, const char **env, size_t env_
     }
     if (env != NULL) {
         for (i = 0; i < env_len; i++) {
+            size_t env_max = 4096;
             if (strlen(env[i]) > env_max) {
                 ERROR("Env is too long");
                 ret = -1;
-- 
2.40.1

