iSulad/0023-improve-code-in-utils.c.patch
zhangxiaoyu 97df963d71 add ut and bugfix for device mapper and websocket
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
2022-11-01 20:06:15 +08:00

165 lines
4.9 KiB
Diff

From 60ef94806076e728b6f76d5b9b874e375182233c Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 18 Oct 2022 15:47:36 +0800
Subject: [PATCH 23/39] improve code in utils.c
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/utils/cutils/utils.c | 60 ++++++++++++++++++++++------------------
1 file changed, 33 insertions(+), 27 deletions(-)
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index e362581b..eddfda5c 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -64,13 +64,13 @@ int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize
void *tmp = NULL;
if (newptr == NULL || newsize == 0) {
- goto err_out;
+ return -1;
}
tmp = util_common_calloc_s(newsize);
if (tmp == NULL) {
ERROR("Failed to malloc memory");
- goto err_out;
+ return -1;
}
if (oldptr != NULL) {
@@ -82,9 +82,6 @@ int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize
*newptr = tmp;
return 0;
-
-err_out:
- return -1;
}
static int util_read_pipe(int pipe_fd, char **out_buf, size_t *out_buf_size, size_t *out_real_size)
@@ -353,8 +350,6 @@ void util_contain_errmsg(const char *errmsg, int *exit_code)
} else if (strcasestr(errmsg, "not a directory")) {
*exit_code = 127;
}
-
- return;
}
char *util_short_digest(const char *digest)
@@ -818,6 +813,9 @@ bool util_exec_cmd(exec_func_t cb_func, void *args, const char *stdin_msg, char
close(in_fd[1]);
in_fd[1] = -1;
+ if (stdout_msg == NULL) {
+ stdout_close_flag = 1;
+ }
for (;;) {
if (stdout_close_flag == 0) {
stdout_close_flag = util_read_pipe(out_fd[0], &stdout_buffer, &stdout_buf_size, &stdout_real_size);
@@ -840,8 +838,14 @@ bool util_exec_cmd(exec_func_t cb_func, void *args, const char *stdin_msg, char
close(err_fd[0]);
close(out_fd[0]);
out:
- *stdout_msg = stdout_buffer;
- *stderr_msg = stderr_buffer;
+ if (stdout_msg != NULL) {
+ *stdout_msg = stdout_buffer;
+ }
+ if (stderr_msg != NULL) {
+ *stderr_msg = stderr_buffer;
+ } else {
+ free(stderr_buffer);
+ }
return ret;
}
@@ -947,8 +951,8 @@ int util_env_insert(char ***penv, size_t *penv_len, const char *key, size_t key_
env = *penv;
env_len = *penv_len;
- if (env_len > (SIZE_MAX / sizeof(char *)) - 1) {
- ERROR("Failed to realloc memory for envionment variables");
+ if (env_len > (MAX_MEMORY_SIZE / sizeof(char *)) - 1) {
+ ERROR("Too large envionment variables");
return -1;
}
@@ -1108,7 +1112,7 @@ static int set_echo_back(bool echo_back)
return 0;
}
-int util_input_notty(char *buf, size_t maxlen)
+static int util_input_notty(char *buf, size_t maxlen)
{
size_t i = 0;
int ret = 0;
@@ -1424,9 +1428,9 @@ int util_read_pid_ppid_info(uint32_t pid, pid_ppid_info_t *pid_info)
proc_t *proc = NULL;
proc_t *p_proc = NULL;
- if (pid == 0) {
- ret = -1;
- goto out;
+ if (pid == 0 || pid_info == NULL) {
+ ERROR("Invalid arguments");
+ return -1;
}
proc = util_get_process_proc_info((pid_t)pid);
@@ -1506,8 +1510,8 @@ defs_map_string_object *dup_map_string_empty_object(defs_map_string_object *src)
return NULL;
}
- dst->keys = util_common_calloc_s(src->len * sizeof(char *));
- dst->values = util_common_calloc_s(src->len * sizeof(defs_map_string_object_element *));
+ dst->keys = util_smart_calloc_s(sizeof(char *), src->len);
+ dst->values = util_smart_calloc_s(sizeof(defs_map_string_object_element *), src->len);
if (dst->keys == NULL || dst->values == NULL) {
ERROR("Out of memory");
ret = -1;
@@ -1538,12 +1542,16 @@ int convert_v2_runtime(const char *runtime, char *binary)
size_t parts_len = 0;
char buf[PATH_MAX] = {0};
int ret = 0;
+ int nret;
+
+ if (binary == NULL) {
+ return -1;
+ }
parts = util_string_split_multi(runtime, '.');
if (parts == NULL) {
ERROR("split failed: %s", runtime);
- ret = -1;
- goto out;
+ return -1;
}
parts_len = util_array_len((const char **)parts);
@@ -1553,15 +1561,13 @@ int convert_v2_runtime(const char *runtime, char *binary)
goto out;
}
- if (binary != NULL) {
- int nret = snprintf(buf, sizeof(buf), "%s-%s-%s-%s", "containerd", "shim", parts[2], parts[3]);
- if (nret < 0 || (size_t)nret >= sizeof(buf)) {
- ERROR("Failed to snprintf string");
- ret = -1;
- goto out;
- }
- strcpy(binary, buf);
+ nret = snprintf(buf, sizeof(buf), "%s-%s-%s-%s", "containerd", "shim", parts[2], parts[3]);
+ if (nret < 0 || (size_t)nret >= sizeof(buf)) {
+ ERROR("Failed to snprintf string");
+ ret = -1;
+ goto out;
}
+ (void)strcpy(binary, buf);
out:
util_free_array(parts);
--
2.25.1