From 42bd2f35eac04a9d8db3aedc8d4e5c709db47cb3 Mon Sep 17 00:00:00 2001 From: liuwentan Date: Wed, 3 Jan 2024 16:49:53 +0800 Subject: [PATCH] [PATCH]validate log_handle in maat_new --- deps/log/log.c | 44 ++++++++++++++++++++++---------------------- src/maat_api.c | 10 ++++++++-- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/deps/log/log.c b/deps/log/log.c index 51bdc9c..6e192d0 100644 --- a/deps/log/log.c +++ b/deps/log/log.c @@ -59,18 +59,14 @@ static int log_create_dir(const char *dir_path, int path_len) return -1; } - char *buf = (char *)calloc(path_len + 1, 1); - int ret = -1; + char *buf = ALLOC(char, path_len + 1); + int ret = 0; memcpy(buf, dir_path, path_len); if (access(buf, R_OK) != 0) { if (mkdir(buf, 0755) != 0) { ret = -1; - } else { - ret = 0; } - } else { - ret = 1; } free(buf); @@ -105,22 +101,22 @@ int log_open_file(char *file_name, struct log_handle *handle) static int log_create_path(const char *file_path) { - FILE *fp = NULL; - - if (file_path == NULL) - return 0; + if (NULL == file_path) { + return -1; + } char *p_path = rindex(file_path, '/'); - if (p_path == 0) { - return 0; + if (NULL == p_path) { + return -1; } const char *p_cur = file_path; int path_len = p_path - file_path; int i = 0; - if (log_create_dir(file_path, path_len) >= 0) - return 0; + if (log_create_dir(file_path, path_len) < 0) { + return -1; + } for (; i <= path_len; i++, p_cur++) { if (*p_cur == '/') { @@ -129,13 +125,14 @@ static int log_create_path(const char *file_path) } } - if (NULL == (fp = fopen(file_path, "w"))) { - return 0; + FILE *fp = fopen(file_path, "w"); + if (NULL == fp) { + return -1; } fclose(fp); - return 1; + return 0; } int log_create_log_file(struct log_handle *handle) @@ -225,17 +222,20 @@ void log_handle_set_enable(struct log_handle * handle, int enable) struct log_handle *log_handle_create(const char *file_path, int level) { struct log_handle *handle = ALLOC(struct log_handle, 1); - if (!handle) { + if (NULL == handle) { return NULL; } + handle->enable = 1; handle->level = level; strncpy(handle->defined_log_fn, file_path, 1024); - pthread_mutex_init(&handle->mutex, NULL); - - if (handle->enable) { - log_create_path(handle->defined_log_fn); + + int ret = log_create_path(handle->defined_log_fn); + if (ret < 0) { + free(handle); + return NULL; } + pthread_mutex_init(&handle->mutex, NULL); return handle; } diff --git a/src/maat_api.c b/src/maat_api.c index 313ac1a..79e2395 100644 --- a/src/maat_api.c +++ b/src/maat_api.c @@ -347,6 +347,7 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path) return NULL; } + int garbage_gc_timeout_s = 0; struct maat *maat_inst = ALLOC(struct maat, 1); maat_inst->opts = *opts; @@ -364,6 +365,11 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path) maat_inst->logger = log_handle_create(log_path, maat_inst->opts.log_level); } + if (NULL == maat_inst->logger) { + fprintf(stderr, "create log handle failed.\n"); + goto failed; + } + if (0 == strlen(maat_inst->opts.foreign_cont_dir)) { snprintf(maat_inst->opts.foreign_cont_dir, sizeof(maat_inst->opts.foreign_cont_dir), "%s_files", table_info_path); @@ -381,8 +387,8 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path) maat_inst->maat_version = 0; maat_inst->last_full_version = 0; - int garbage_gc_timeout_s = (maat_inst->opts.rule_effect_interval_ms / 1000) + - (maat_inst->opts.gc_timeout_ms / 1000); + garbage_gc_timeout_s = (maat_inst->opts.rule_effect_interval_ms / 1000) + + (maat_inst->opts.gc_timeout_ms / 1000); if (maat_inst->opts.input_mode != DATA_SOURCE_IRIS_FILE && maat_inst->opts.input_mode != DATA_SOURCE_JSON_FILE &&