From 948ed9928d933bbbfbb704ab0cc2296cb4333055 Mon Sep 17 00:00:00 2001 From: lti9hc <114125133+lti9hc@users.noreply.github.com> Date: Wed, 26 Oct 2022 15:11:24 +0700 Subject: Fix for Resource and Memory Leak (#418) dlt_daemon_client.c Adding NULL check for tok dlt_daemon_offline_logstorage.c : Adding NULL check for application dlt_user.c : Fix for Memory Leak dlt-daemon.c : Fix for Resource Leak dlt_config_file_parser.c : Add termination character at the end of string dlt_offline_trace.c : Fix for Resource Leak Signed-off-by: Mvaradaraj2 manoj.varadaraj2@harman.com Co-authored-by: Le Tin --- src/daemon/dlt-daemon.c | 4 ++- src/daemon/dlt_daemon_client.c | 2 +- src/daemon/dlt_daemon_offline_logstorage.c | 9 +++++-- src/lib/dlt_user.c | 41 +++++++++++++++++------------- src/shared/dlt_config_file_parser.c | 1 + src/shared/dlt_offline_trace.c | 3 +++ 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index 2c9ff6a..c8a8506 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -1009,7 +1009,7 @@ int main(int argc, char *argv[]) dlt_log(LOG_ERR, "Could not load runtime config\n"); return -1; } - + /* * Load dlt-runtime.cfg if available. * This must be loaded before offline setup @@ -2157,6 +2157,8 @@ int dlt_daemon_process_client_connect(DltDaemon *daemon, if (dlt_daemon_send_ringbuffer_to_client(daemon, daemon_local, verbose) == -1) { dlt_log(LOG_WARNING, "Can't send contents of ringbuffer to clients\n"); + close(in_sock); + in_sock = -1; return -1; } diff --git a/src/daemon/dlt_daemon_client.c b/src/daemon/dlt_daemon_client.c index 82e2965..9ebbd23 100644 --- a/src/daemon/dlt_daemon_client.c +++ b/src/daemon/dlt_daemon_client.c @@ -1085,7 +1085,7 @@ void dlt_daemon_control_get_log_info(int sock, daemon->ecuid, verbose); - if (application) { + if ((user_list->applications) && (application)) { /* Calculate start offset within contexts[] */ offset_base = 0; diff --git a/src/daemon/dlt_daemon_offline_logstorage.c b/src/daemon/dlt_daemon_offline_logstorage.c index 5d21a76..c796d06 100644 --- a/src/daemon/dlt_daemon_offline_logstorage.c +++ b/src/daemon/dlt_daemon_offline_logstorage.c @@ -233,9 +233,14 @@ DLT_STATIC DltReturnValue dlt_logstorage_split_multi(char *key, else { strncpy(ecuid, tok, DLT_ID_SIZE); tok = strtok(NULL, ":"); - strncpy(apid, tok, DLT_ID_SIZE); + + if (tok != NULL) + strncpy(apid, tok, DLT_ID_SIZE); + tok = strtok(NULL, ":"); - strncpy(ctid, tok, DLT_ID_SIZE); + + if (tok != NULL) + strncpy(ctid, tok, DLT_ID_SIZE); } return DLT_RETURN_OK; diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 99df11b..0085593 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -1813,27 +1813,34 @@ DltReturnValue dlt_user_log_write_start_internal(DltContext *handle, ret = dlt_user_log_write_start_init(handle, log, loglevel, is_verbose); if (ret == DLT_RETURN_TRUE) { /* initialize values */ - if (log->buffer == NULL) { + if ((NULL != log->buffer)) + { + free(log->buffer); + log->buffer = NULL; + } + else + { log->buffer = calloc(sizeof(unsigned char), dlt_user.log_buf_len); - - if (log->buffer == NULL) { - dlt_vlog(LOG_ERR, "Cannot allocate buffer for DLT Log message\n"); - return DLT_RETURN_ERROR; - } } - /* In non-verbose mode, insert message id */ - if (!is_verbose_mode(dlt_user.verbose_mode, log)) { - if ((sizeof(uint32_t)) > dlt_user.log_buf_len) { - return DLT_RETURN_USER_BUFFER_FULL; - } + if (log->buffer == NULL) { + dlt_vlog(LOG_ERR, "Cannot allocate buffer for DLT Log message\n"); + return DLT_RETURN_ERROR; + } + else + { + /* In non-verbose mode, insert message id */ + if (!is_verbose_mode(dlt_user.verbose_mode, log)) { + if ((sizeof(uint32_t)) > dlt_user.log_buf_len) + return DLT_RETURN_USER_BUFFER_FULL; - /* Write message id */ - memcpy(log->buffer, &(messageid), sizeof(uint32_t)); - log->size = sizeof(uint32_t); + /* Write message id */ + memcpy(log->buffer, &(messageid), sizeof(uint32_t)); + log->size = sizeof(uint32_t); - /* as the message id is part of each message in non-verbose mode, - * it doesn't increment the argument counter in extended header (if used) */ + /* as the message id is part of each message in non-verbose mode, + * it doesn't increment the argument counter in extended header (if used) */ + } } } @@ -1874,7 +1881,7 @@ DltReturnValue dlt_user_log_write_start_w_given_buffer(DltContext *handle, return ret; } - + DltReturnValue dlt_user_log_write_finish(DltContextData *log) { int ret = DLT_RETURN_ERROR; diff --git a/src/shared/dlt_config_file_parser.c b/src/shared/dlt_config_file_parser.c index fc2d516..72c57ab 100644 --- a/src/shared/dlt_config_file_parser.c +++ b/src/shared/dlt_config_file_parser.c @@ -496,6 +496,7 @@ int dlt_config_file_get_section_name(const DltConfigFile *file, return -1; strncpy(name, (file->sections + num)->name, DLT_CONFIG_FILE_ENTRY_MAX_LEN); + name[DLT_CONFIG_FILE_ENTRY_MAX_LEN - 1] = '\0'; return 0; } diff --git a/src/shared/dlt_offline_trace.c b/src/shared/dlt_offline_trace.c index 2d70a77..2b1e0df 100644 --- a/src/shared/dlt_offline_trace.c +++ b/src/shared/dlt_offline_trace.c @@ -296,6 +296,9 @@ int dlt_offline_trace_delete_oldest_file(DltOfflineTrace *trace) /* go through all dlt files in directory */ DIR *dir = opendir(trace->directory); + if(!dir) + return -1; + while ((dp = readdir(dir)) != NULL) if (strstr(dp->d_name, DLT_OFFLINETRACE_FILENAME_BASE)) { int res = snprintf(filename, sizeof(filename), "%s/%s", trace->directory, dp->d_name); -- cgit v1.2.1