From c60b974f2b3279b132d7562071a9b9c6549366d1 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 22 Aug 2023 14:25:17 +0800
Subject: [PATCH 05/10] ensure argument of interface is not null

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 src/common/constants.h                        |  2 +-
 src/daemon/modules/container/container_unix.c | 15 +++--------
 .../container/health_check/health_check.c     |  3 ++-
 .../leftover_cleanup/clean_context.c          |  2 +-
 .../restart_manager/restartmanager.c          |  2 +-
 .../modules/container/supervisor/supervisor.c |  3 ++-
 src/daemon/modules/events/collector.c         |  4 +--
 src/daemon/modules/image/oci/registry/auths.c |  3 +++
 src/daemon/modules/log/log_gather.c           |  5 ++++
 src/daemon/modules/plugin/plugin.c            | 18 +++++++++++--
 src/daemon/modules/runtime/runtime.c          | 18 ++++++-------
 src/daemon/modules/runtime/shim/shim_rt_ops.c | 10 +++++--
 src/utils/buffer/buffer.c                     |  6 ++++-
 src/utils/console/console.c                   |  5 ++++
 src/utils/cutils/filters.c                    |  1 +
 src/utils/cutils/mainloop.c                   | 26 ++++++++++++++++++-
 src/utils/cutils/path.c                       |  4 +++
 src/utils/cutils/utils.c                      | 14 ++++++++++
 src/utils/cutils/utils_file.c                 | 26 +++++++++++++++----
 src/utils/cutils/utils_fs.c                   | 16 ++++++++++++
 src/utils/cutils/utils_network.c              |  8 +++++-
 src/utils/cutils/utils_string.c               |  8 ++++++
 src/utils/cutils/utils_timestamp.c            |  6 ++++-
 src/utils/http/http.c                         |  5 ++++
 src/utils/http/parser.c                       |  4 +--
 src/utils/tar/isulad_tar.c                    | 16 ++++++++++--
 src/utils/tar/util_archive.c                  |  4 +--
 src/utils/tar/util_gzip.c                     |  8 ++++++
 test/cutils/utils_file/utils_file_ut.cc       |  3 +++
 29 files changed, 196 insertions(+), 49 deletions(-)

diff --git a/src/common/constants.h b/src/common/constants.h
index 37854291..e968d8cd 100644
--- a/src/common/constants.h
+++ b/src/common/constants.h
@@ -42,7 +42,7 @@ extern "C" {
 
 #define SECURE_CONFIG_FILE_MODE 0600
 
-#define ARCH_LOG_FILE_MODE 0440
+#define ARCH_LOG_FILE_MODE 0400
 
 #define WORKING_LOG_FILE_MODE 0640
 
diff --git a/src/daemon/modules/container/container_unix.c b/src/daemon/modules/container/container_unix.c
index 9392cf0d..d9706f08 100644
--- a/src/daemon/modules/container/container_unix.c
+++ b/src/daemon/modules/container/container_unix.c
@@ -410,16 +410,11 @@ static int pack_path_and_args_from_container_spec(const container_config *contai
         v2_spec->path = util_strdup_s(container_spec->cmd[0]);
         total = container_spec->cmd_len - 1;
 
-        if (total > SIZE_MAX / sizeof(char *)) {
-            ERROR("Container oci spec process args elements is too much!");
-            ret = -1;
-            goto out;
-        }
         if (total == 0) {
             goto out;
         }
 
-        v2_spec->args = util_common_calloc_s(total * sizeof(char *));
+        v2_spec->args = util_smart_calloc_s(sizeof(char *), total);
         if (v2_spec->args == NULL) {
             ERROR("Out of memory");
             ret = -1;
@@ -1137,19 +1132,15 @@ int container_exit_on_next(container_t *cont)
 /* this function should be called in container_lock*/
 int container_wait_stop(container_t *cont, int timeout)
 {
-    int ret = 0;
-
     if (cont == NULL) {
         return -1;
     }
 
     if (!container_is_running(cont->state)) {
-        goto unlock;
+        return 0;
     }
 
-    ret = container_wait_stop_cond_wait(cont, timeout);
-unlock:
-    return ret;
+    return container_wait_stop_cond_wait(cont, timeout);
 }
 
 /* container wait stop locking */
diff --git a/src/daemon/modules/container/health_check/health_check.c b/src/daemon/modules/container/health_check/health_check.c
index e9dcbdb9..2b840228 100644
--- a/src/daemon/modules/container/health_check/health_check.c
+++ b/src/daemon/modules/container/health_check/health_check.c
@@ -376,6 +376,7 @@ static void *stop_container_on_unhealthy(void *arg)
     ret = pthread_detach(pthread_self());
     if (ret != 0) {
         CRIT("Set thread detach fail");
+        return NULL;
     }
 
     if (arg == NULL) {
@@ -603,7 +604,7 @@ static void health_check_run(const char *container_id)
     cont = containers_store_get(container_id);
     if (cont == NULL) {
         ERROR("Failed to get container info");
-        goto out;
+        return;
     }
 
     config = cont->common_config->config;
diff --git a/src/daemon/modules/container/leftover_cleanup/clean_context.c b/src/daemon/modules/container/leftover_cleanup/clean_context.c
index 6ccc39ed..517d3cbd 100644
--- a/src/daemon/modules/container/leftover_cleanup/clean_context.c
+++ b/src/daemon/modules/container/leftover_cleanup/clean_context.c
@@ -62,7 +62,7 @@ void clean_ctx_fill_broken_rootfs(struct clean_ctx *ctx, const char *id)
     struct linked_list *new_node = NULL;
     char *broken_id = NULL;
 
-    if (!ctx->inited) {
+    if (ctx == NULL || !ctx->inited) {
         return;
     }
 
diff --git a/src/daemon/modules/container/restart_manager/restartmanager.c b/src/daemon/modules/container/restart_manager/restartmanager.c
index 7bd0f4d5..e5fffcb6 100644
--- a/src/daemon/modules/container/restart_manager/restartmanager.c
+++ b/src/daemon/modules/container/restart_manager/restartmanager.c
@@ -431,7 +431,7 @@ bool restart_manager_should_restart(const char *id, uint32_t exit_code, bool has
     restart_manager_set_items(rm, exit_code, exec_duration);
 
     restart = should_be_restart(rm, exit_code, has_been_manually_stopped);
-    if (restart) {
+    if (restart && timeout != NULL) {
         *timeout = (uint64_t)rm->timeout;
     }
 
diff --git a/src/daemon/modules/container/supervisor/supervisor.c b/src/daemon/modules/container/supervisor/supervisor.c
index d5997b63..3ce4ec1e 100644
--- a/src/daemon/modules/container/supervisor/supervisor.c
+++ b/src/daemon/modules/container/supervisor/supervisor.c
@@ -170,6 +170,7 @@ static void *clean_resources_thread(void *arg)
     ret = pthread_detach(pthread_self());
     if (ret != 0) {
         CRIT("Set thread detach fail");
+        return NULL;
     }
 
     prctl(PR_SET_NAME, "Clean resource");
@@ -217,7 +218,7 @@ retry:
 }
 
 /* new clean resources thread */
-int new_clean_resources_thread(struct supervisor_handler_data *data)
+static int new_clean_resources_thread(struct supervisor_handler_data *data)
 {
     int ret = 0;
     pthread_t clean_thread;
diff --git a/src/daemon/modules/events/collector.c b/src/daemon/modules/events/collector.c
index b3c7622a..3c1eae1a 100644
--- a/src/daemon/modules/events/collector.c
+++ b/src/daemon/modules/events/collector.c
@@ -514,8 +514,6 @@ out:
 static void events_append(const struct isulad_events_format *event)
 {
     struct isulad_events_format *tmpevent = NULL;
-    struct linked_list *newnode = NULL;
-    struct linked_list *firstnode = NULL;
 
     if (pthread_mutex_lock(&g_events_buffer.event_mutex)) {
         WARN("Failed to lock");
@@ -523,6 +521,7 @@ static void events_append(const struct isulad_events_format *event)
     }
 
     if (g_events_buffer.size < EVENTSLIMIT) {
+        struct linked_list *newnode = NULL;
         newnode = util_common_calloc_s(sizeof(struct linked_list));
         if (newnode == NULL) {
             CRIT("Memory allocation error.");
@@ -547,6 +546,7 @@ static void events_append(const struct isulad_events_format *event)
         linked_list_add_tail(&g_events_buffer.event_list, newnode);
         g_events_buffer.size++;
     } else {
+        struct linked_list *firstnode = NULL;
         firstnode = linked_list_first_node(&g_events_buffer.event_list);
         if (firstnode != NULL) {
             linked_list_del(firstnode);
diff --git a/src/daemon/modules/image/oci/registry/auths.c b/src/daemon/modules/image/oci/registry/auths.c
index 1fbe2bd3..98202cf6 100644
--- a/src/daemon/modules/image/oci/registry/auths.c
+++ b/src/daemon/modules/image/oci/registry/auths.c
@@ -103,6 +103,9 @@ static int decode_auth_aes(char *encoded, char **username, char **password)
         goto out;
     }
 
+    free(*username);
+    util_free_sensitive_string(*password);
+
     *username = util_strdup_s(auth_parts[0]);
     *password = util_strdup_s(auth_parts[1]);
     (void)memset(auth_parts[0], 0, strlen(auth_parts[0]));
diff --git a/src/daemon/modules/log/log_gather.c b/src/daemon/modules/log/log_gather.c
index 414c9ad1..8c19f33b 100644
--- a/src/daemon/modules/log/log_gather.c
+++ b/src/daemon/modules/log/log_gather.c
@@ -89,6 +89,11 @@ static int file_rotate_me(const char *file_name)
         return -1;
     }
 
+    if (chmod(tmp_path, ARCH_LOG_FILE_MODE) != 0) {
+        ERROR("Change mode of %s failed", tmp_path);
+        return -1;
+    }
+
     if (gzip(tmp_path, sizeof(tmp_path))) {
         WARN("Gzip file failed");
         return -2;
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 1c0af368..e08479ab 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -794,7 +794,7 @@ static int plugin_set_activated(plugin_t *plugin, bool activated, const char *er
 
 int plugin_set_manifest(plugin_t *plugin, const plugin_manifest_t *manifest)
 {
-    if (manifest == NULL) {
+    if (plugin == NULL || manifest == NULL) {
         return -1;
     }
 
@@ -974,7 +974,7 @@ static bool plugin_useby_container(const plugin_t *plugin, const container_t *co
     }
 
     free(plugin_names);
-    free(pnames);
+    util_free_array(pnames);
     return ok;
 }
 
@@ -1175,6 +1175,11 @@ out:
 int pm_add_plugin(plugin_t *plugin)
 {
     int ok = 0;
+
+    if (plugin == NULL) {
+        return -1;
+    }
+
     pm_wrlock();
     ok = map_insert(g_plugin_manager->np, (void *)plugin->name, plugin);
     pm_unlock();
@@ -1191,6 +1196,11 @@ int pm_add_plugin(plugin_t *plugin)
 int pm_del_plugin(const plugin_t *plugin)
 {
     int ok;
+
+    if (plugin == NULL) {
+        return -1;
+    }
+
     pm_wrlock();
     /* plugin_put() called in map_remove() by pm_np_item_free() */
     ok = map_remove(g_plugin_manager->np, (void *)plugin->name);
@@ -1204,6 +1214,10 @@ int pm_del_plugin(const plugin_t *plugin)
 
 int pm_get_plugin(const char *name, plugin_t **rplugin)
 {
+    if (rplugin == NULL) {
+        return -1;
+    }
+
     if (do_get_plugin(name, rplugin) == 0) {
         return 0;
     }
diff --git a/src/daemon/modules/runtime/runtime.c b/src/daemon/modules/runtime/runtime.c
index 93ce987f..f2222315 100644
--- a/src/daemon/modules/runtime/runtime.c
+++ b/src/daemon/modules/runtime/runtime.c
@@ -121,7 +121,7 @@ int runtime_create(const char *name, const char *runtime, const rt_create_params
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalide arguments for runtime create");
         ret = -1;
         goto out;
@@ -145,7 +145,7 @@ int runtime_start(const char *name, const char *runtime, const rt_start_params_t
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL || pid_info == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL || pid_info == NULL) {
         ERROR("Invalide arguments for runtime start");
         ret = -1;
         goto out;
@@ -169,7 +169,7 @@ int runtime_kill(const char *name, const char *runtime, const rt_kill_params_t *
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalid arguments for runtime kill");
         ret = -1;
         goto out;
@@ -193,7 +193,7 @@ int runtime_restart(const char *name, const char *runtime, const rt_restart_para
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalide arguments for runtime restart");
         ret = -1;
         goto out;
@@ -217,7 +217,7 @@ int runtime_clean_resource(const char *name, const char *runtime, const rt_clean
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalide arguments for runtime clean");
         ret = -1;
         goto out;
@@ -241,7 +241,7 @@ int runtime_rm(const char *name, const char *runtime, const rt_rm_params_t *para
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalide arguments for runtime rm");
         ret = -1;
         goto out;
@@ -266,7 +266,7 @@ int runtime_status(const char *name, const char *runtime, const rt_status_params
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL || status == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL || status == NULL) {
         ERROR("Invalide arguments for runtime status");
         ret = -1;
         goto out;
@@ -291,7 +291,7 @@ int runtime_resources_stats(const char *name, const char *runtime, const rt_stat
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL || rs_stats == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL || rs_stats == NULL) {
         ERROR("Invalide arguments for runtime stats");
         ret = -1;
         goto out;
@@ -315,7 +315,7 @@ int runtime_exec(const char *name, const char *runtime, const rt_exec_params_t *
     int ret = 0;
     const struct rt_ops *ops = NULL;
 
-    if (name == NULL || runtime == NULL || exit_code == NULL) {
+    if (name == NULL || runtime == NULL || params == NULL || exit_code == NULL) {
         ERROR("Invalide arguments for runtime exec");
         ret = -1;
         goto out;
diff --git a/src/daemon/modules/runtime/shim/shim_rt_ops.c b/src/daemon/modules/runtime/shim/shim_rt_ops.c
index 9afb9ce7..8cdf0138 100644
--- a/src/daemon/modules/runtime/shim/shim_rt_ops.c
+++ b/src/daemon/modules/runtime/shim/shim_rt_ops.c
@@ -188,12 +188,14 @@ static int shim_bin_v2_create(const char *runtime, const char *id, const char *w
     }
 
     close(exec_fd[1]);
+    exec_fd[1] = -1;
     if (util_read_nointr(exec_fd[0], exec_buff, sizeof(exec_buff) - 1) > 0) {
         ERROR("exec failed: %s", exec_buff);
         ret = -1;
         goto out;
     }
     close(exec_fd[0]);
+    exec_fd[0] = -1;
 
     status = util_wait_for_pid_status(pid);
     if (status < 0) {
@@ -207,9 +209,13 @@ static int shim_bin_v2_create(const char *runtime, const char *id, const char *w
     close(out_fd[1]);
     util_read_nointr(out_fd[0], stdout_buff, sizeof(stdout_buff) - 1);
     close(out_fd[0]);
+    out_fd[0] = -1;
+    out_fd[1] = -1;
     close(err_fd[1]);
     util_read_nointr(err_fd[0], stderr_buff, sizeof(stderr_buff) - 1);
     close(err_fd[0]);
+    err_fd[0] = -1;
+    err_fd[1] = -1;
 
     if (status != 0) {
         ERROR("shim-v2 binary %d exit in %d with %s, %s", pid, status, stdout_buff, stderr_buff);
@@ -342,7 +348,7 @@ int rt_shim_clean_resource(const char *id, const char *runtime, const rt_clean_p
     int ret = 0;
     int nret = 0;
     char workdir[PATH_MAX] = {0};
-    struct DeleteResponse res = {};
+    struct DeleteResponse res = {0};
 
     if (id == NULL || runtime == NULL || params == NULL) {
         ERROR("Invalid input params");
@@ -518,7 +524,7 @@ int rt_shim_status(const char *id, const char *runtime, const rt_status_params_t
     char address[PATH_MAX] = {0};
     int ret = 0;
     int nret = 0;
-    struct State ss = {};
+    struct State ss = {0};
 
     if (id == NULL || params == NULL || status == NULL) {
         ERROR("Invalid input params");
diff --git a/src/utils/buffer/buffer.c b/src/utils/buffer/buffer.c
index 7f6bc527..172809a2 100644
--- a/src/utils/buffer/buffer.c
+++ b/src/utils/buffer/buffer.c
@@ -78,7 +78,7 @@ void buffer_empty(Buffer *buf)
 }
 
 /* buffer grow */
-int buffer_grow(Buffer *buffer, size_t min_size)
+static int buffer_grow(Buffer *buffer, size_t min_size)
 {
     size_t factor = 0;
     size_t new_size = 0;
@@ -128,6 +128,10 @@ int buffer_append(Buffer *buf, const char *append, size_t len)
         return -1;
     }
 
+    if (append == NULL || len == 0) {
+        return 0;
+    }
+
     desired_length = len + 1;
     if ((buf->total_size - buf->bytes_used) < desired_length) {
         int status = buffer_grow(buf, desired_length);
diff --git a/src/utils/console/console.c b/src/utils/console/console.c
index d5e5d9af..a160d685 100644
--- a/src/utils/console/console.c
+++ b/src/utils/console/console.c
@@ -208,6 +208,11 @@ int console_fifo_create(const char *fifo_path)
 {
     int ret;
 
+    if (fifo_path == NULL || strlen(fifo_path) > PATH_MAX) {
+        ERROR("Invalid input!");
+        return -1;
+    }
+
     ret = mknod(fifo_path, S_IFIFO | S_IRUSR | S_IWUSR, (dev_t)0);
     if (ret < 0 && errno != EEXIST) {
         ERROR("Failed to mknod monitor fifo %s: %s.", fifo_path, strerror(errno));
diff --git a/src/utils/cutils/filters.c b/src/utils/cutils/filters.c
index 43ccde20..bb0d81be 100644
--- a/src/utils/cutils/filters.c
+++ b/src/utils/cutils/filters.c
@@ -287,6 +287,7 @@ bool filters_args_match(const struct filters_args *filters, const char *field, c
     map_t *field_values_map = NULL;
     map_itor *itor = NULL;
 
+    // if filters == NULL, filters_args_exact_match will return true
     if (filters_args_exact_match(filters, field, source)) {
         return true;
     }
diff --git a/src/utils/cutils/mainloop.c b/src/utils/cutils/mainloop.c
index 1028087a..365d1e53 100644
--- a/src/utils/cutils/mainloop.c
+++ b/src/utils/cutils/mainloop.c
@@ -37,6 +37,10 @@ int epoll_loop(struct epoll_descr *descr, int t)
     struct epoll_loop_handler *epoll_handler = NULL;
     struct epoll_event evs[MAX_EVENTS];
 
+    if (descr == NULL) {
+        return -1;
+    }
+
     while (1) {
         int ep_fds = epoll_wait(descr->fd, evs, MAX_EVENTS, t);
         if (ep_fds < 0) {
@@ -77,6 +81,10 @@ int epoll_loop_add_handler(struct epoll_descr *descr, int fd, epoll_loop_callbac
     struct epoll_loop_handler *epoll_handler = NULL;
     struct linked_list *node = NULL;
 
+    if (descr == NULL) {
+        return -1;
+    }
+
     epoll_handler = util_common_calloc_s(sizeof(*epoll_handler));
     if (epoll_handler == NULL) {
         goto fail_out;
@@ -114,6 +122,10 @@ int epoll_loop_del_handler(struct epoll_descr *descr, int fd)
     struct epoll_loop_handler *epoll_handler = NULL;
     struct linked_list *index = NULL;
 
+    if (descr == NULL) {
+        return -1;
+    }
+
     linked_list_for_each(index, &descr->handler_list) {
         epoll_handler = index->elem;
 
@@ -136,6 +148,10 @@ fail_out:
 /* epoll loop open */
 int epoll_loop_open(struct epoll_descr *descr)
 {
+    if (descr == NULL) {
+        return -1;
+    }
+
     descr->fd = epoll_create1(EPOLL_CLOEXEC);
     if (descr->fd < 0) {
         return -1;
@@ -152,6 +168,11 @@ int epoll_loop_close(struct epoll_descr *descr)
 {
     struct linked_list *index = NULL;
     struct linked_list *next = NULL;
+    int ret = 0;
+
+    if (descr == NULL) {
+        return ret;
+    }
 
     linked_list_for_each_safe(index, &(descr->handler_list), next) {
         linked_list_del(index);
@@ -159,5 +180,8 @@ int epoll_loop_close(struct epoll_descr *descr)
         free(index);
     }
 
-    return close(descr->fd);
+    ret = close(descr->fd);
+    descr->fd = -1;
+
+    return ret;
 }
diff --git a/src/utils/cutils/path.c b/src/utils/cutils/path.c
index 5a743632..d586e981 100644
--- a/src/utils/cutils/path.c
+++ b/src/utils/cutils/path.c
@@ -567,6 +567,10 @@ char *util_get_resource_path(const char *rootpath, const char *path)
     char tmppath[PATH_MAX] = { 0 };
     char fullpath[PATH_MAX] = { 0 };
 
+    if (rootpath == NULL) {
+        return NULL;
+    }
+
     nret = snprintf(tmppath, sizeof(tmppath), "/%s/%s", rootpath, path);
     if (nret < 0 || (size_t)nret >= sizeof(tmppath)) {
         return NULL;
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index a29de20e..d628e1f6 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -1182,6 +1182,10 @@ int util_input_readall(char *buf, size_t maxlen)
     size_t i = 0;
     int ret = 0;
 
+    if (buf == NULL) {
+        return 0;
+    }
+
     for (;;) {
         int c = getchar();
         if (c == EOF) {
@@ -1234,12 +1238,18 @@ static int util_input(char *buf, size_t maxlen, bool echo_back)
 // Get input from stdin, echo back if get any character.
 int util_input_echo(char *buf, size_t maxlen)
 {
+    if (buf == NULL) {
+        return 0;
+    }
     return util_input(buf, maxlen, true);
 }
 
 // Get input from stdin, no echo back.
 int util_input_noecho(char *buf, size_t maxlen)
 {
+    if (buf == NULL) {
+        return 0;
+    }
     return util_input(buf, maxlen, false);
 }
 
@@ -1269,6 +1279,10 @@ int util_generate_random_str(char *id, size_t len)
     size_t i;
     const int m = 256;
 
+    if (id == NULL) {
+        return -1;
+    }
+
     len = len / 2;
     fd = open("/dev/urandom", O_RDONLY);
     if (fd == -1) {
diff --git a/src/utils/cutils/utils_file.c b/src/utils/cutils/utils_file.c
index 9000b0dc..5fa556f3 100644
--- a/src/utils/cutils/utils_file.c
+++ b/src/utils/cutils/utils_file.c
@@ -41,7 +41,7 @@
 #include "utils_array.h"
 #include "utils_string.h"
 
-int copy_dir_recursive(char *copy_dst, char *copy_src, map_t *inodes);
+static int copy_dir_recursive(char *copy_dst, char *copy_src, map_t *inodes);
 static void do_calculate_dir_size_without_hardlink(const char *dirpath, int recursive_depth, int64_t *total_size,
                                                    int64_t *total_inode, map_t *map);
 
@@ -303,14 +303,18 @@ out:
 
 bool util_force_remove_file(const char *fname, int *saved_errno)
 {
+    if (fname == NULL) {
+        return true;
+    }
+
     if (unlink(fname) == 0) {
         return true;
     }
 
-    WARN("Failed to delete %s: %s", fname, strerror(errno));
-    if (*saved_errno == 0) {
+    if (saved_errno != NULL && *saved_errno == 0) {
         *saved_errno = errno;
     }
+    WARN("Failed to delete %s: %s", fname, strerror(errno));
 
     if (mark_file_mutable(fname) != 0) {
         WARN("Failed to mark file mutable");
@@ -711,6 +715,10 @@ int util_gzip_compressed(const char *filename, bool *gzip)
     FILE *f = NULL;
     int ret = 0;
 
+    if (filename == NULL || gzip == NULL) {
+        return -1;
+    }
+
     f = fopen(filename, "rb");
     if (f == NULL) {
         ERROR("Failed to open file %s: %s", filename, strerror(errno));
@@ -1663,7 +1671,7 @@ int util_proc_file_line_by_line(FILE *fp, read_line_callback_t cb, void *context
     ssize_t nret = 0;
     int ret = 0;
 
-    if (fp == NULL) {
+    if (fp == NULL || cb == NULL) {
         ERROR("Invalid parameter");
         return -1;
     }
@@ -1730,6 +1738,10 @@ int util_recursive_remove_path(const char *path)
 {
     int ret = 0;
 
+    if (path == NULL) {
+        return -1;
+    }
+
     if (unlink(path) != 0 && errno != ENOENT) {
         ret = util_recursive_rmdir(path, 0);
     }
@@ -2053,6 +2065,10 @@ int util_copy_dir_recursive(char *copy_dst, char *copy_src)
     int ret = 0;
     map_t *inodes = NULL;
 
+    if (copy_dst == NULL || copy_src == NULL) {
+        return -1;
+    }
+
     // key: source inode, value: target file path
     inodes = map_new(MAP_INT_STR, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
     if (inodes == NULL) {
@@ -2067,7 +2083,7 @@ int util_copy_dir_recursive(char *copy_dst, char *copy_src)
     return ret;
 }
 
-int copy_dir_recursive(char *copy_dst, char *copy_src, map_t *inodes)
+static int copy_dir_recursive(char *copy_dst, char *copy_src, map_t *inodes)
 {
     char **entries = NULL;
     size_t entry_num = 0;
diff --git a/src/utils/cutils/utils_fs.c b/src/utils/cutils/utils_fs.c
index a8c65f86..ae71f3ba 100644
--- a/src/utils/cutils/utils_fs.c
+++ b/src/utils/cutils/utils_fs.c
@@ -362,6 +362,10 @@ bool util_detect_mounted(const char *path)
     size_t length = 0;
     bool bret = false;
 
+    if (path == NULL) {
+        return false;
+    }
+
     fp = util_fopen("/proc/self/mountinfo", "r");
     if (fp == NULL) {
         ERROR("Failed opening /proc/self/mountinfo");
@@ -396,6 +400,10 @@ bool util_deal_with_mount_info(mount_info_call_back_t cb, const char *pattern)
     bool bret = true;
     int nret = 0;
 
+    if (cb == NULL) {
+        return false;
+    }
+
     fp = util_fopen("/proc/self/mountinfo", "r");
     if (fp == NULL) {
         ERROR("Failed opening /proc/self/mountinfo");
@@ -582,6 +590,10 @@ int util_mount_from(const char *base, const char *src, const char *dst, const ch
     pid_t pid = -1;
     int keepfds[] = { -1 };
 
+    if (base == NULL || src == NULL || dst == NULL || mtype == NULL) {
+        return -1;
+    }
+
     pid = fork();
     if (pid == (pid_t) -1) {
         ERROR("Failed to fork: %s", strerror(errno));
@@ -628,6 +640,10 @@ bool util_check_readonly_fs(const char *path)
     const int max_retry = 10;
     struct statfs fsbuf;
 
+    if (path == NULL) {
+        return false;
+    }
+
     for (i = 0; i < max_retry; i++) {
         if (statfs(path, &fsbuf) == 0) {
             break;
diff --git a/src/utils/cutils/utils_network.c b/src/utils/cutils/utils_network.c
index c77edc3c..7f67b326 100644
--- a/src/utils/cutils/utils_network.c
+++ b/src/utils/cutils/utils_network.c
@@ -102,6 +102,10 @@ int util_mount_namespace(const char *netns_path)
     int ret = 0;
     void *status = NULL;
 
+    if (netns_path == NULL) {
+        return -1;
+    }
+
     ret = pthread_create(&newns_thread, NULL, mount_netns, (void *)netns_path);
     if (ret != 0) {
         ERROR("Failed to create thread");
@@ -135,8 +139,10 @@ out:
 int util_umount_namespace(const char *netns_path)
 {
     int i = 0;
+
     if (netns_path == NULL) {
-        WARN("Invalid path to umount");
+        WARN("Empty netns path to umount");
+        return 0;
     }
 
     for (i = 0; i < 50; i++) {
diff --git a/src/utils/cutils/utils_string.c b/src/utils/cutils/utils_string.c
index ba7dd5b4..64afb570 100644
--- a/src/utils/cutils/utils_string.c
+++ b/src/utils/cutils/utils_string.c
@@ -190,6 +190,10 @@ int util_parse_size_int_and_float(const char *numstr, int64_t mlt, int64_t *conv
     char *dot = NULL;
     int nret;
 
+    if (numstr == NULL || converted == NULL) {
+        return -1;
+    }
+
     dot = strchr(numstr, '.');
     if (dot != NULL) {
         char tmp;
@@ -824,6 +828,10 @@ int util_string_array_unique(const char **elements, size_t length, char ***uniqu
     char **tmp_elements = NULL;
     size_t tmp_elements_len = 0;
 
+    if (unique_elements == NULL || unique_elements_len == NULL) {
+        return -1;
+    }
+
     if (elements == NULL || length == 0) {
         return 0;
     }
diff --git a/src/utils/cutils/utils_timestamp.c b/src/utils/cutils/utils_timestamp.c
index 3a440ca9..2f378078 100644
--- a/src/utils/cutils/utils_timestamp.c
+++ b/src/utils/cutils/utils_timestamp.c
@@ -214,6 +214,10 @@ out:
 /* get time buffer */
 bool util_get_time_buffer(const types_timestamp_t *timestamp, char *timebuffer, size_t maxsize)
 {
+    if (timestamp == NULL) {
+        return false;
+    }
+
     return get_time_buffer_help(timestamp, timebuffer, maxsize, false);
 }
 
@@ -382,7 +386,7 @@ bool util_parsing_time(const char *format, const char *time, struct tm *tm, int3
     size_t len_time = 0;
     size_t index_nanos = 0;
 
-    if (format == NULL || time == NULL) {
+    if (format == NULL || time == NULL || tm == NULL || nanos == NULL) {
         return false;
     }
 
diff --git a/src/utils/http/http.c b/src/utils/http/http.c
index 2b77c422..47d17455 100644
--- a/src/utils/http/http.c
+++ b/src/utils/http/http.c
@@ -528,6 +528,11 @@ int authz_http_request(const char *username, const char *action, char **resp)
     int nret = 0;
     size_t length = 0;
     struct http_get_options *options = NULL;
+
+    if (username == NULL || action == NULL || resp == NULL) {
+        return -1;
+    }
+
     if (strlen(username) > ((SIZE_MAX - strlen(action)) - strlen(":")) - 1) {
         ERROR("Invalid arguments");
         return -1;
diff --git a/src/utils/http/parser.c b/src/utils/http/parser.c
index 30b26a00..5f61d336 100644
--- a/src/utils/http/parser.c
+++ b/src/utils/http/parser.c
@@ -307,8 +307,7 @@ int parse_http(const char *buf, size_t len, struct parsed_http_message *m,
 
     parser = parser_init(type, m);
     if (parser == NULL) {
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     nparsed = parse(buf, len, parser);
@@ -320,7 +319,6 @@ int parse_http(const char *buf, size_t len, struct parsed_http_message *m,
 
 free_out:
     parser_free(parser);
-out:
     return ret;
 }
 
diff --git a/src/utils/tar/isulad_tar.c b/src/utils/tar/isulad_tar.c
index d7d69eb2..7264282f 100644
--- a/src/utils/tar/isulad_tar.c
+++ b/src/utils/tar/isulad_tar.c
@@ -97,7 +97,7 @@ cleanup:
     return ret;
 }
 
-int resolve_host_source_path(const char *path, bool follow_link, char **resolved_path, char **rebase_name, char **err)
+static int resolve_host_source_path(const char *path, bool follow_link, char **resolved_path, char **rebase_name, char **err)
 {
     int ret = -1;
     int nret = 0;
@@ -174,6 +174,10 @@ struct archive_copy_info *copy_info_source_path(const char *path, bool follow_li
     char *resolved_path = NULL;
     char *rebase_name = NULL;
 
+    if (path == NULL || err == NULL) {
+        return NULL;
+    }
+
     info = util_common_calloc_s(sizeof(struct archive_copy_info));
     if (info == NULL) {
         ERROR("Out of memory");
@@ -283,7 +287,7 @@ cleanup:
     return -1;
 }
 
-struct archive_copy_info *copy_info_destination_path(const char *path, char **err)
+static struct archive_copy_info *copy_info_destination_path(const char *path, char **err)
 {
     struct archive_copy_info *info = NULL;
     struct stat st;
@@ -389,6 +393,10 @@ int archive_copy_to(const struct io_read_wrapper *content, const struct archive_
     char *src_base = NULL;
     char *dst_base = NULL;
 
+    if (err == NULL || dstpath == NULL || srcinfo == NULL || content == NULL) {
+        return -1;
+    }
+
     dstinfo = copy_info_destination_path(dstpath, err);
     if (dstinfo == NULL) {
         ERROR("Can not get destination info: %s", dstpath);
@@ -444,5 +452,9 @@ cleanup:
 
 int tar_resource(const struct archive_copy_info *info, struct io_read_wrapper *archive_reader, char **err)
 {
+    if (info == NULL || archive_reader == NULL || err == NULL) {
+        return -1;
+    }
+
     return tar_resource_rebase(info->path, info->rebase_name, archive_reader, err);
 }
diff --git a/src/utils/tar/util_archive.c b/src/utils/tar/util_archive.c
index c63fd00b..08116589 100644
--- a/src/utils/tar/util_archive.c
+++ b/src/utils/tar/util_archive.c
@@ -66,7 +66,7 @@ struct archive_content_data {
     char buff[ARCHIVE_READ_BUFFER_SIZE];
 };
 
-ssize_t read_content(struct archive *a, void *client_data, const void **buff)
+static ssize_t read_content(struct archive *a, void *client_data, const void **buff)
 {
     struct archive_content_data *mydata = client_data;
 
@@ -535,8 +535,6 @@ static void try_to_replace_exited_dst(const char *dst_path, struct archive_entry
     if (util_recursive_remove_path(dst_path) != 0) {
         ERROR("Failed to remove path %s while unpack", dst_path);
     }
-
-    return;
 }
 
 int archive_unpack_handler(const struct io_read_wrapper *content, const struct archive_options *options)
diff --git a/src/utils/tar/util_gzip.c b/src/utils/tar/util_gzip.c
index 2665e6df..bf649300 100644
--- a/src/utils/tar/util_gzip.c
+++ b/src/utils/tar/util_gzip.c
@@ -36,6 +36,10 @@ int util_gzip_z(const char *srcfile, const char *dstfile, const mode_t mode)
     const char *gzerr = NULL;
     int errnum = 0;
 
+    if (srcfile == NULL || dstfile == NULL) {
+        return -1;
+    }
+
     srcfd = util_open(srcfile, O_RDONLY, SECURE_CONFIG_FILE_MODE);
     if (srcfd < 0) {
         ERROR("Open src file: %s, failed: %s", srcfile, strerror(errno));
@@ -105,6 +109,10 @@ int util_gzip_d(const char *srcfile, const FILE *dstfp)
     size_t size = 0;
     void *buffer = NULL;
 
+    if (srcfile == NULL || dstfp == NULL) {
+        return -1;
+    }
+
     stream = gzopen(srcfile, "r");
     if (stream == NULL) {
         ERROR("gzopen %s failed: %s", srcfile, strerror(errno));
diff --git a/test/cutils/utils_file/utils_file_ut.cc b/test/cutils/utils_file/utils_file_ut.cc
index 78c07e60..cacfef45 100644
--- a/test/cutils/utils_file/utils_file_ut.cc
+++ b/test/cutils/utils_file/utils_file_ut.cc
@@ -197,8 +197,11 @@ TEST(utils_file, test_util_proc_file_line_by_line)
     fp = util_fopen(path.c_str(), "r");
     checked_layers = map_new(MAP_STR_BOOL, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
     ASSERT_EQ(util_proc_file_line_by_line(fp, parse_checked_layer_cb, (void *)checked_layers), 0);
+    ASSERT_EQ(util_proc_file_line_by_line(fp, nullptr, (void *)checked_layers), -1);
     fclose(fp);
     ASSERT_EQ(util_path_remove(path.c_str()), 0);
+    
+    ASSERT_EQ(util_proc_file_line_by_line(nullptr, parse_checked_layer_cb, (void *)checked_layers), -1);
 }
 
 TEST(utils_file, test_util_recursive_remove_path)
-- 
2.25.1

