From 56c2a6a98d51ea893939079cc31e3a7897fa5aba Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 31 May 2022 12:53:10 +0100
Subject: [PATCH 08/30] add check to arguments

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 src/cmd/isula/base/create.c                   |  7 ++-
 src/cmd/isula/stream/attach.c                 |  5 ++
 src/cmd/isulad/isulad_commands.c              |  2 +-
 src/daemon/common/err_msg.c                   |  5 +-
 src/daemon/common/selinux_label.c             |  7 ++-
 src/daemon/common/sysinfo.c                   |  9 +++
 src/daemon/config/isulad_config.c             |  9 +++
 src/daemon/modules/api/plugin_api.h           |  1 -
 src/daemon/modules/image/image.c              |  6 ++
 .../modules/image/oci/oci_common_operators.c  |  2 +-
 .../graphdriver/devmapper/deviceset.c         |  5 ++
 .../graphdriver/devmapper/metadata_store.c    |  4 ++
 src/daemon/modules/plugin/plugin.c            | 58 ++-----------------
 src/utils/console/console.c                   | 10 ++++
 src/utils/cutils/filters.c                    |  2 +-
 src/utils/cutils/map/rb_tree.c                |  4 ++
 src/utils/cutils/path.c                       |  5 ++
 src/utils/cutils/utils.c                      | 48 +++++++--------
 src/utils/http/http.c                         |  7 ++-
 src/utils/http/parser.c                       |  5 ++
 test/path/path_ut.cc                          |  5 ++
 21 files changed, 120 insertions(+), 86 deletions(-)

diff --git a/src/cmd/isula/base/create.c b/src/cmd/isula/base/create.c
index ce485f0d..2db2fd21 100644
--- a/src/cmd/isula/base/create.c
+++ b/src/cmd/isula/base/create.c
@@ -393,7 +393,7 @@ static int read_label_from_file(const char *path, size_t file_size, isula_contai
     if (file_size == 0) {
         return 0;
     }
-    fp = fopen(path, "re");
+    fp = util_fopen(path, "re");
     if (fp == NULL) {
         ERROR("Failed to open '%s'", path);
         return -1;
@@ -1477,12 +1477,13 @@ int callback_log_opt(command_option_t *option, const char *value)
 
 int callback_log_driver(command_option_t *option, const char *value)
 {
-    struct client_arguments *args = (struct client_arguments *)option->data;
+    struct client_arguments *args = NULL;
 
-    if (value == NULL) {
+    if (value == NULL || option == NULL) {
         COMMAND_ERROR("log driver is NULL");
         return -1;
     }
+    args = (struct client_arguments *)option->data;
 
     if (!check_opt_container_log_driver(value)) {
         COMMAND_ERROR("Unsupported log driver: %s", value);
diff --git a/src/cmd/isula/stream/attach.c b/src/cmd/isula/stream/attach.c
index 3a434bca..9cc19161 100644
--- a/src/cmd/isula/stream/attach.c
+++ b/src/cmd/isula/stream/attach.c
@@ -104,6 +104,11 @@ int inspect_container(const struct client_arguments *args, container_inspect **i
     isula_connect_ops *ops = NULL;
     parser_error perr = NULL;
 
+    if (inspect_data == NULL) {
+        COMMAND_ERROR("Empty inspect data");
+        return -1;
+    }
+
     inspect_response = util_common_calloc_s(sizeof(struct isula_inspect_response));
     if (inspect_response == NULL) {
         COMMAND_ERROR("Out of memory");
diff --git a/src/cmd/isulad/isulad_commands.c b/src/cmd/isulad/isulad_commands.c
index f73a82e2..89d91c1b 100644
--- a/src/cmd/isulad/isulad_commands.c
+++ b/src/cmd/isulad/isulad_commands.c
@@ -657,7 +657,7 @@ static int check_conf_default_ulimit(const struct service_arguments *args)
             ret = -1;
             goto out;
         }
-        if (strcmp(ptr->name, type) != 0) {
+        if (type == NULL || strcmp(ptr->name, type) != 0) {
             COMMAND_ERROR("Ulimit Name \"%s\" must same as type \"%s\" in %s", ptr->name, type,
                           ISULAD_DAEMON_JSON_CONF_FILE);
             ret = -1;
diff --git a/src/daemon/common/err_msg.c b/src/daemon/common/err_msg.c
index 739cb353..3e62fad6 100644
--- a/src/daemon/common/err_msg.c
+++ b/src/daemon/common/err_msg.c
@@ -29,8 +29,11 @@ void isulad_set_error_message(const char *format, ...)
 {
     int ret = 0;
     char errbuf[BUFSIZ + 1] = { 0 };
-
     va_list argp;
+
+    if (format == NULL) {
+        return;
+    }
     va_start(argp, format);
 
     ret = vsnprintf(errbuf, BUFSIZ, format, argp);
diff --git a/src/daemon/common/selinux_label.c b/src/daemon/common/selinux_label.c
index 5468111e..533393a6 100644
--- a/src/daemon/common/selinux_label.c
+++ b/src/daemon/common/selinux_label.c
@@ -602,7 +602,7 @@ static int container_label(char **process_label, char **file_label)
         return -1;
     }
 
-    file = fopen(lxc_path, "re");
+    file = util_fopen(lxc_path, "re");
     if (file == NULL) {
         ERROR("Failed to open '%s'", lxc_path);
         return -1;
@@ -782,6 +782,11 @@ int init_label(const char **label_opts, size_t label_opts_len, char **dst_proces
         return 0;
     }
 
+    if (label_opts == NULL || dst_process_label == NULL || dst_mount_label == NULL) {
+        ERROR("Empty arguments");
+        return -1;
+    }
+
     if (container_label(&process_label, &mount_label) != 0) {
         ret = -1;
         goto out;
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index 6fb4ec38..89ca5225 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -915,6 +915,11 @@ int find_cgroup_mountpoint_and_root(const char *subsystem, char **mountpoint, ch
     size_t length = 0;
     char *pline = NULL;
 
+    if (subsystem == NULL) {
+        ERROR("Empty subsystem");
+        return -1;
+    }
+
     fp = util_fopen("/proc/self/mountinfo", "r");
     if (fp == NULL) {
         ERROR("Failed to open \"/proc/self/mountinfo\"\n");
@@ -1566,6 +1571,10 @@ mountinfo_t *find_mount_info(mountinfo_t **minfos, const char *dir)
 {
     mountinfo_t **it = NULL;
 
+    if (dir == NULL) {
+        return NULL;
+    }
+
     for (it = minfos; it && *it; it++) {
         if ((*it)->mountpoint && !strcmp((*it)->mountpoint, dir)) {
             return *it;
diff --git a/src/daemon/config/isulad_config.c b/src/daemon/config/isulad_config.c
index 92d86a3b..38d2a0bf 100644
--- a/src/daemon/config/isulad_config.c
+++ b/src/daemon/config/isulad_config.c
@@ -227,6 +227,10 @@ int conf_get_cgroup_cpu_rt(int64_t *cpu_rt_period, int64_t *cpu_rt_runtime)
 {
     struct service_arguments *conf = NULL;
 
+    if (cpu_rt_period == NULL || cpu_rt_runtime == NULL) {
+        return -1;
+    }
+
     if (isulad_server_conf_rdlock() != 0) {
         return -1;
     }
@@ -776,6 +780,11 @@ out:
 
 int conf_get_daemon_log_config(char **loglevel, char **logdriver, char **engine_log_path)
 {
+    if (loglevel == NULL || logdriver == NULL || engine_log_path == NULL) {
+        ERROR("Empty arguments");
+        return -1;
+    }
+
     *loglevel = conf_get_isulad_loglevel();
     if (*loglevel == NULL) {
         ERROR("DoStart: Failed to get log level");
diff --git a/src/daemon/modules/api/plugin_api.h b/src/daemon/modules/api/plugin_api.h
index 4346b9e9..303ba6bf 100644
--- a/src/daemon/modules/api/plugin_api.h
+++ b/src/daemon/modules/api/plugin_api.h
@@ -101,7 +101,6 @@ int pm_del_plugin(const plugin_t *plugin);
  */
 int pm_get_plugin(const char *name, plugin_t **rplugin);
 void pm_put_plugin(plugin_t *plugin);
-int pm_get_plugins_nolock(uint64_t pe, plugin_t ***rplugins, size_t *count);
 
 int start_plugin_manager(void);
 int plugin_event_container_pre_create(const char *cid, oci_runtime_spec *ocic);
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index 6832aec3..f487f831 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -831,11 +831,17 @@ static int append_images_to_response(im_list_response *response, imagetool_image
 out:
     return ret;
 }
+
 int im_list_images(const im_list_request *ctx, im_list_response **response)
 {
     size_t i;
     imagetool_images_list *images_tmp = NULL;
 
+    if (response == NULL) {
+        ERROR("Empty arguments");
+        return -1;
+    }
+
     *response = util_common_calloc_s(sizeof(im_list_response));
     if (*response == NULL) {
         ERROR("Out of memory");
diff --git a/src/daemon/modules/image/oci/oci_common_operators.c b/src/daemon/modules/image/oci/oci_common_operators.c
index 09405651..fd23096a 100644
--- a/src/daemon/modules/image/oci/oci_common_operators.c
+++ b/src/daemon/modules/image/oci/oci_common_operators.c
@@ -459,7 +459,7 @@ int oci_status_image(im_status_request *request, im_status_response *response)
     char *image_ref = NULL;
     char *resolved_name = NULL;
 
-    if (response == NULL) {
+    if (request == NULL || response == NULL) {
         ERROR("Invalid arguments");
         return -1;
     }
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 e20d4f1b..d90dde50 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
@@ -3183,6 +3183,11 @@ struct status *device_set_status(struct device_set *devset)
     int sem_usz = 0;
     int sem_mni = 0;
 
+    if (devset == NULL) {
+        ERROR("empty arguments");
+        return NULL;
+    }
+
     if (pthread_rwlock_wrlock(&(devset->devmapper_driver_rwlock)) != 0) {
         ERROR("lock devmapper conf failed");
         return NULL;
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/metadata_store.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/metadata_store.c
index c8d10b7a..93fc9758 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/metadata_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/metadata_store.c
@@ -201,6 +201,10 @@ char **metadata_store_list_hashes(metadata_store_t *meta_store)
     char **hashes_array = NULL;
     map_itor *itor = NULL;
 
+    if (meta_store == NULL) {
+        return NULL;
+    }
+
     if (map_size(meta_store->map) == 0) {
         DEBUG("Metadata store hash list is empty");
         ret = true;
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 4cea2b2a..4e343a20 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -760,8 +760,14 @@ plugin_t *plugin_new(const char *name, const char *addr)
     plugin_t *plugin = NULL;
     int errcode = 0;
 
+    if (name == NULL || addr == NULL) {
+        ERROR("Empty arguments");
+        return NULL;
+    }
+
     plugin = util_common_calloc_s(sizeof(plugin_t));
     if (plugin == NULL) {
+        ERROR("Out of memory");
         goto bad;
     }
 
@@ -1234,58 +1240,6 @@ void pm_put_plugin(plugin_t *plugin)
     plugin_put(plugin);
 }
 
-int pm_get_plugins_nolock(uint64_t pe, plugin_t ***rplugins, size_t *count)
-{
-    int ret = 0;
-    int i = 0;
-    size_t size = 0;
-    plugin_t **plugins = NULL;
-    map_itor *itor = NULL;
-
-    size = map_size(g_plugin_manager->np);
-    if (size == 0) { /* empty */
-        return 0;
-    }
-    if (size > SIZE_MAX / sizeof(plugin_t *)) {
-        ret = -1;
-        ERROR("Invalid plugins size");
-        goto out;
-    }
-
-    plugins = util_common_calloc_s(sizeof(plugin_t *) * size);
-    if (plugins == NULL) {
-        ret = -1;
-        ERROR("Out of memory");
-        goto out;
-    }
-
-    itor = map_itor_new(g_plugin_manager->np);
-    if (itor == NULL) {
-        ret = -1;
-        ERROR("Out of memory");
-        goto out;
-    }
-
-    for (i = 0; i < (int)size && map_itor_valid(itor); i++, map_itor_next(itor)) {
-        plugins[i] = map_itor_value(itor);
-        /* plugin_put() called in pm_put_plugins() */
-        plugin_get(plugins[i]);
-    }
-
-    *rplugins = plugins;
-    *count = (size_t)i;
-
-out:
-    map_itor_free(itor);
-    itor = NULL;
-
-    if (ret < 0) {
-        UTIL_FREE_AND_SET_NULL(plugins);
-    }
-
-    return ret;
-}
-
 static void pm_np_item_free(void *key, void *val)
 {
     plugin_t *plugin = val;
diff --git a/src/utils/console/console.c b/src/utils/console/console.c
index 17c8b242..8492eb4d 100644
--- a/src/utils/console/console.c
+++ b/src/utils/console/console.c
@@ -253,6 +253,11 @@ int console_fifo_open(const char *fifo_path, int *fdout, int flags)
 {
     int fd = 0;
 
+    if (fifo_path ==NULL || fdout == NULL) {
+        ERROR("Argument must not be NULL");
+        return -1;
+    }
+
     fd = util_open(fifo_path, flags, (mode_t)0);
     if (fd < 0) {
         ERROR("Failed to open fifo %s to send message: %s.", fifo_path, strerror(errno));
@@ -302,6 +307,11 @@ int setup_tios(int fd, struct termios *curr_tios)
     struct termios tmptios;
     int ret = 0;
 
+    if (curr_tios == NULL) {
+        ERROR("Empty terminal io setting");
+        return -1;
+    }
+
     if (!isatty(fd)) {
         ERROR("Specified fd: '%d' is not a tty", fd);
         return -1;
diff --git a/src/utils/cutils/filters.c b/src/utils/cutils/filters.c
index a8fa220f..1885e9cc 100644
--- a/src/utils/cutils/filters.c
+++ b/src/utils/cutils/filters.c
@@ -66,7 +66,7 @@ char **filters_args_get(const struct filters_args *filters, const char *field)
     map_t *field_values_map = NULL;
     map_itor *itor = NULL;
 
-    if (filters == NULL || filters->fields == NULL) {
+    if (filters == NULL || filters->fields == NULL || field == NULL) {
         return NULL;
     }
 
diff --git a/src/utils/cutils/map/rb_tree.c b/src/utils/cutils/map/rb_tree.c
index ddae0646..b63165d5 100644
--- a/src/utils/cutils/map/rb_tree.c
+++ b/src/utils/cutils/map/rb_tree.c
@@ -133,6 +133,10 @@ void rbtree_clear(rb_tree_t *tree)
 
 void rbtree_free(rb_tree_t *tree)
 {
+    if (tree == NULL) {
+        return;
+    }
+
     rbtree_clear(tree);
     free(tree->nil);
     tree->nil = NULL;
diff --git a/src/utils/cutils/path.c b/src/utils/cutils/path.c
index f195257d..2446f479 100644
--- a/src/utils/cutils/path.c
+++ b/src/utils/cutils/path.c
@@ -590,6 +590,11 @@ int util_resolve_path(const char *rootpath, const char *path, char **resolvedpat
     char tmppath[PATH_MAX] = { 0 };
     char cleanedpath[PATH_MAX] = { 0 };
 
+    if (abspath == NULL || resolvedpath == NULL || rootpath == NULL) {
+        ERROR("Empty arguments");
+        return -1;
+    }
+
     *resolvedpath = NULL;
     *abspath = NULL;
 
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index 278a72c5..30ff629f 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -1475,32 +1475,32 @@ void util_parse_user_group(const char *username, char **user, char **group, char
         return;
     }
 
-    if (username != NULL) {
-        tmp = util_strdup_s(username);
-
-        // for free tmp in caller
-        *tmp_dup = tmp;
-
-        pdot = strstr(tmp, ":");
-        if (pdot != NULL) {
-            *pdot = '\0';
-            if (pdot != tmp) {
-                // User found
-                *user = tmp;
-            }
-            if (*(pdot + 1) != '\0') {
-                // group found
-                *group = pdot + 1;
-            }
-        } else {
-            // No : found
-            if (*tmp != '\0') {
-                *user = tmp;
-            }
-        }
+    if (username == NULL) {
+        return;
     }
 
-    return;
+    tmp = util_strdup_s(username);
+
+    // for free tmp in caller
+    *tmp_dup = tmp;
+
+    pdot = strstr(tmp, ":");
+    if (pdot != NULL) {
+        *pdot = '\0';
+        if (pdot != tmp) {
+            // User found
+            *user = tmp;
+        }
+        if (*(pdot + 1) != '\0') {
+            // group found
+            *group = pdot + 1;
+        }
+    } else {
+        // No : found
+        if (*tmp != '\0') {
+            *user = tmp;
+        }
+    }
 }
 
 defs_map_string_object *dup_map_string_empty_object(defs_map_string_object *src)
diff --git a/src/utils/http/http.c b/src/utils/http/http.c
index 0b53cf1e..e4788efd 100644
--- a/src/utils/http/http.c
+++ b/src/utils/http/http.c
@@ -241,7 +241,7 @@ static void http_custom_general_options(CURL *curl_handle, const struct http_get
 
 static int http_custom_options(CURL *curl_handle, const struct http_get_options *options)
 {
-    if (curl_handle == NULL || options == NULL) {
+    if (curl_handle == NULL) {
         return -1;
     }
 
@@ -413,6 +413,11 @@ int http_request(const char *url, struct http_get_options *options, long *respon
     size_t fsize = 0;
     char *replaced_url = 0;
 
+    if (url == NULL || options == NULL) {
+        ERROR("must set url and options to use http request");
+        return -1;
+    }
+
     if (recursive_len + 1 >= MAX_REDIRCT_NUMS) {
         ERROR("reach the max redirect num");
         return -1;
diff --git a/src/utils/http/parser.c b/src/utils/http/parser.c
index 37475b33..0e0e603b 100644
--- a/src/utils/http/parser.c
+++ b/src/utils/http/parser.c
@@ -319,6 +319,11 @@ char *get_header_value(const struct parsed_http_message *m, const char *header)
     int i = 0;
     char *ret = NULL;
 
+    if (m == NULL || header == NULL) {
+        ERROR("Empty arguments");
+        return NULL;
+    }
+
     for (i = 0; i < m->num_headers; i++) {
         if (strcasecmp(m->headers[i][0], header) == 0) {
             ret = (char *)m->headers[i][1];
diff --git a/test/path/path_ut.cc b/test/path/path_ut.cc
index ce6dcc08..0068ecb4 100644
--- a/test/path/path_ut.cc
+++ b/test/path/path_ut.cc
@@ -311,6 +311,11 @@ TEST(path_ut, test_resolve_path)
     char *resolvedpath = nullptr;
     char *abspath = nullptr;
 
+    rootpath = "/home";
+    path = "/home/dir/test";
+    ASSERT_EQ(util_resolve_path(rootpath.c_str(), path.c_str(), nullptr, &abspath), -1);
+    ASSERT_EQ(util_resolve_path(rootpath.c_str(), path.c_str(), &resolvedpath, nullptr), -1);
+
     ASSERT_EQ(util_resolve_path(nullptr, nullptr, &resolvedpath, &abspath), -1);
     free(resolvedpath);
     resolvedpath = nullptr;
-- 
2.32.1 (Apple Git-133)

