From 6088448cdfe3cd152c53136544acc4ee7a508a84 Mon Sep 17 00:00:00 2001 From: Saya Sugiura Date: Wed, 12 Jun 2019 16:18:02 +0900 Subject: Fix alerts from lgtm Signed-off-by: Saya Sugiura --- include/dlt/dlt_filetransfer.h | 5 +++++ src/console/dlt-control-common.c | 12 ++++++------ src/console/dlt-sortbytimestamp.c | 5 +++-- src/daemon/dlt_daemon_client.c | 16 ++++++++-------- src/daemon/dlt_daemon_common.c | 8 +++----- src/lib/dlt_client.c | 2 +- src/lib/dlt_filetransfer.c | 21 +++++++++++++-------- src/lib/dlt_user.c | 4 ++-- .../dlt_offline_logstorage_behavior.c | 15 ++++++++------- src/shared/dlt_common.c | 18 ++++++++---------- src/shared/dlt_offline_trace.c | 11 +++-------- src/system/dlt-system-journal.c | 2 +- 12 files changed, 61 insertions(+), 58 deletions(-) diff --git a/include/dlt/dlt_filetransfer.h b/include/dlt/dlt_filetransfer.h index 1cc96cc..89a2e25 100644 --- a/include/dlt/dlt_filetransfer.h +++ b/include/dlt/dlt_filetransfer.h @@ -24,6 +24,9 @@ * \file dlt_filetransfer.h */ +#ifndef DLT_FILETRANSFER_H +#define DLT_FILETRANSFER_H + #include /* Needed for LONG_MAX */ #include /* Needed for struct stat st*/ #include "dlt.h" /* Needed for DLT Logs */ @@ -135,3 +138,5 @@ extern int dlt_user_log_file_data(DltContext *fileContext, const char *filename, * @return Returns 0 if everything was okey. If there was a failure value < 0 will be returned. */ extern int dlt_user_log_file_end(DltContext *fileContext, const char *filename, int deleteFlag); + +#endif /* DLT_FILETRANSFER_H */ diff --git a/src/console/dlt-control-common.c b/src/console/dlt-control-common.c index 964dbe8..d6787fc 100644 --- a/src/console/dlt-control-common.c +++ b/src/console/dlt-control-common.c @@ -83,7 +83,7 @@ static int (*response_analyzer_cb)(char *, void *, int); static pthread_t daemon_connect_thread; -static DltClient client; +static DltClient g_client; static int callback_return = -1; static pthread_mutex_t answer_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t answer_cond = PTHREAD_COND_INITIALIZER; @@ -466,7 +466,7 @@ static int dlt_control_init_connection(DltClient *client, void *cb) static void *dlt_control_listen_to_daemon(void *data) { pr_verbose("Ready to receive DLT answers.\n"); - dlt_client_main_loop(&client, NULL, get_verbosity()); + dlt_client_main_loop(&g_client, NULL, get_verbosity()); return data; } @@ -567,7 +567,7 @@ int dlt_control_send_message(DltControlMsgBody *body, int timeout) /* Re-init the return value */ callback_return = -1; - if (dlt_control_send_message_to_socket(client.sock, msg) != 0) { + if (dlt_control_send_message_to_socket(g_client.sock, msg) != 0) { pr_error("Sending message to daemon failed\n"); free(msg); return -1; @@ -611,9 +611,9 @@ int dlt_control_init(int (*response_analyzer)(char *, void *, int), set_ecuid(ecuid); set_verbosity(verbosity); - if (dlt_control_init_connection(&client, dlt_control_callback) != 0) { + if (dlt_control_init_connection(&g_client, dlt_control_callback) != 0) { pr_error("Connection initialization failed\n"); - dlt_client_cleanup(&client, get_verbosity()); + dlt_client_cleanup(&g_client, get_verbosity()); return -1; } @@ -640,5 +640,5 @@ int dlt_control_deinit(void) /* Stopping the listener thread */ pthread_cancel(daemon_connect_thread); /* Closing the socket */ - return dlt_client_cleanup(&client, get_verbosity()); + return dlt_client_cleanup(&g_client, get_verbosity()); } diff --git a/src/console/dlt-sortbytimestamp.c b/src/console/dlt-sortbytimestamp.c index c006681..c7b5818 100644 --- a/src/console/dlt-sortbytimestamp.c +++ b/src/console/dlt-sortbytimestamp.c @@ -93,7 +93,8 @@ void verbose(int level, char *msg, ...) time_t tnow = time((time_t *)0); if (tnow != -1) { - char *snow = ctime(&tnow); + char snow[50]; + ctime_r(&tnow, snow); /* suppress newline char */ snow[strlen(snow) - 1] = 0; printf("%s: ", snow); @@ -340,7 +341,7 @@ int main(int argc, char *argv[]) return -1; } - if ((end < 0) || (end < begin) || (end >= file.counter)) { + if (end >= file.counter) { fprintf(stderr, "ERROR: Selected end message %d is out of range!\n", end); return -1; } diff --git a/src/daemon/dlt_daemon_client.c b/src/daemon/dlt_daemon_client.c index fd2bde4..e2a0d12 100644 --- a/src/daemon/dlt_daemon_client.c +++ b/src/daemon/dlt_daemon_client.c @@ -1505,7 +1505,8 @@ void dlt_daemon_control_callsw_cinjection(int sock, DLT_MSG_READ_VALUE(id_tmp, ptr, datalength, uint32_t); /* Get service id */ id = DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp); - if ((id >= DLT_DAEMON_INJECTION_MIN) && (id <= DLT_DAEMON_INJECTION_MAX)) { + /* id is always less than DLT_DAEMON_INJECTION_MAX since its type is uinit32_t */ + if (id >= DLT_DAEMON_INJECTION_MIN) { /* This a a real SW-C injection call */ data_length_inject = 0; data_length_inject_tmp = 0; @@ -2123,7 +2124,6 @@ void dlt_daemon_control_set_timing_packets(int sock, void dlt_daemon_control_message_time(int sock, DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose) { - int ret; DltMessage msg; int32_t len; @@ -2185,12 +2185,12 @@ void dlt_daemon_control_message_time(int sock, DltDaemon *daemon, DltDaemonLocal msg.standardheader->len = DLT_HTOBE_16(((uint16_t)len)); - /* Send message */ - if ((ret = - dlt_daemon_client_send(sock, daemon, daemon_local, msg.headerbuffer, sizeof(DltStorageHeader), - msg.headerbuffer + sizeof(DltStorageHeader), - msg.headersize - sizeof(DltStorageHeader), - msg.databuffer, msg.datasize, verbose))) {} + /* Send message, ignore return value */ + dlt_daemon_client_send(sock, daemon, daemon_local, msg.headerbuffer, + sizeof(DltStorageHeader), + msg.headerbuffer + sizeof(DltStorageHeader), + msg.headersize - sizeof(DltStorageHeader), + msg.databuffer, msg.datasize, verbose); /* free message */ dlt_message_free(&msg, 0); diff --git a/src/daemon/dlt_daemon_common.c b/src/daemon/dlt_daemon_common.c index 38b94fe..4a39d64 100644 --- a/src/daemon/dlt_daemon_common.c +++ b/src/daemon/dlt_daemon_common.c @@ -1120,9 +1120,8 @@ int dlt_daemon_contexts_load(DltDaemon *daemon, const char *filename, int verbos * We need to check here if there was an error or was it feof.*/ if (ferror(fd)) { dlt_vlog(LOG_WARNING, - "%s fgets(buf,sizeof(buf),fd[%d]) returned NULL. %s\n", + "%s fgets(buf,sizeof(buf),fd) returned NULL. %s\n", __func__, - fd, strerror(errno)); fclose(fd); return -1; @@ -1134,9 +1133,8 @@ int dlt_daemon_contexts_load(DltDaemon *daemon, const char *filename, int verbos } else { dlt_vlog(LOG_WARNING, - "%s fgets(buf,sizeof(buf),fd[%d]) returned NULL. Unknown error.\n", - __func__, - fd); + "%s fgets(buf,sizeof(buf),fd) returned NULL. Unknown error.\n", + __func__); fclose(fd); return -1; } diff --git a/src/lib/dlt_client.c b/src/lib/dlt_client.c index dacff27..af7e118 100644 --- a/src/lib/dlt_client.c +++ b/src/lib/dlt_client.c @@ -801,7 +801,7 @@ DltReturnValue dlt_client_send_all_trace_status(DltClient *client, uint8_t trace payload = (uint8_t *)malloc(sizeof(DltServiceSetDefaultLogLevel)); if (payload == 0) { - dlt_vlog(LOG_ERR, "%s: Could not allocate memory %d\n", __func__, sizeof(DltServiceSetDefaultLogLevel)); + dlt_vlog(LOG_ERR, "%s: Could not allocate memory %zu\n", __func__, sizeof(DltServiceSetDefaultLogLevel)); return DLT_RETURN_ERROR; } diff --git a/src/lib/dlt_filetransfer.c b/src/lib/dlt_filetransfer.c index 736a7d6..3304ff6 100644 --- a/src/lib/dlt_filetransfer.c +++ b/src/lib/dlt_filetransfer.c @@ -167,18 +167,19 @@ time_t getFileCreationDate(const char *file, int *ok) * @param file Absolute file path * @return Returns the creation date of a file */ -char *getFileCreationDate2(const char *file, int *ok) +void getFileCreationDate2(const char *file, int *ok, char *date) { struct stat st; + struct tm ts; if (-1 == stat(file, &st)) { *ok = 0; - return 0; + date = 0; } *ok = 1; - struct tm *ts = localtime(&st.st_ctime); - return asctime(ts); + localtime_r(&st.st_ctime, &ts); + asctime_r(&ts, date); } /*!Checks if the file exists */ @@ -249,7 +250,8 @@ void dlt_user_log_file_errorMessage(DltContext *fileContext, const char *filenam DLT_STRING("dlt_user_log_file_errorMessage, error in getFilesize for: "), DLT_STRING(filename)); - char *fcreationdate = getFileCreationDate2(filename, &ok); + char fcreationdate[50]; + getFileCreationDate2(filename, &ok, fcreationdate); if (!ok) DLT_LOG(*fileContext, @@ -313,7 +315,8 @@ int dlt_user_log_file_infoAbout(DltContext *fileContext, const char *filename) DLT_STRING("dlt_user_log_file_infoAbout, Error getting serial number of file:"), DLT_STRING(filename)); - char *creationdate = getFileCreationDate2(filename, &ok); + char creationdate[50]; + getFileCreationDate2(filename, &ok, creationdate); if (!ok) DLT_LOG(*fileContext, @@ -448,7 +451,8 @@ int dlt_user_log_file_header_alias(DltContext *fileContext, const char *filename DLT_LOG(*fileContext, DLT_LOG_ERROR, DLT_STRING("dlt_user_log_file_header_alias, Error getting size of file:"), DLT_STRING(filename)); - char *fcreationdate = getFileCreationDate2(filename, &ok); + char fcreationdate[50]; + getFileCreationDate2(filename, &ok, fcreationdate); if (!ok) DLT_LOG(*fileContext, DLT_LOG_ERROR, @@ -503,7 +507,8 @@ int dlt_user_log_file_header(DltContext *fileContext, const char *filename) DLT_STRING("dlt_user_log_file_header, Error getting size of file:"), DLT_STRING(filename)); - char *fcreationdate = getFileCreationDate2(filename, &ok); + char fcreationdate[50]; + getFileCreationDate2(filename, &ok, fcreationdate); if (!ok) DLT_LOG(*fileContext, DLT_LOG_ERROR, diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 7c8ca90..dbaf7f1 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -2065,7 +2065,7 @@ DltReturnValue dlt_user_log_write_ptr(DltContextData *log, void *data) return DLT_RETURN_WRONG_PARAMETER; if (!dlt_user_initialised) { - dlt_vlog(LOG_WARNING, "%user_initialised false\n", __FUNCTION__); + dlt_vlog(LOG_WARNING, "%s user_initialised false\n", __FUNCTION__); return DLT_RETURN_ERROR; } @@ -2815,7 +2815,7 @@ void dlt_user_trace_network_segmented_thread(void *unused) /* This case will not happen. */ /* When this thread is interrupted by signal, mq_receive() will not return */ /* partial read length and will return -1. And also no data is removed from mq. */ - dlt_vlog(LOG_WARNING, "NWTSegmented: Could not read data fully from queue: %d\n", read); + dlt_vlog(LOG_WARNING, "NWTSegmented: Could not read data fully from queue: %zd\n", read); continue; } diff --git a/src/offlinelogstorage/dlt_offline_logstorage_behavior.c b/src/offlinelogstorage/dlt_offline_logstorage_behavior.c index 2637936..b9dc26c 100644 --- a/src/offlinelogstorage/dlt_offline_logstorage_behavior.c +++ b/src/offlinelogstorage/dlt_offline_logstorage_behavior.c @@ -89,16 +89,17 @@ void dlt_logstorage_log_file_name(char *log_file_name, if (file_config->logfile_timestamp) { char stamp[DLT_OFFLINE_LOGSTORAGE_TIMESTAMP_LEN + 1] = { 0 }; time_t t = time(NULL); - struct tm *tm_info = localtime(&t); + struct tm tm_info; + localtime_r(&t, &tm_info); sprintf(stamp, "%c%04d%02d%02d-%02d%02d%02d", file_config->logfile_delimiter, - 1900 + tm_info->tm_year, - 1 + tm_info->tm_mon, - tm_info->tm_mday, - tm_info->tm_hour, - tm_info->tm_min, - tm_info->tm_sec); + 1900 + tm_info.tm_year, + 1 + tm_info.tm_mon, + tm_info.tm_mday, + tm_info.tm_hour, + tm_info.tm_min, + tm_info.tm_sec); strcat(log_file_name, stamp); } diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 0e8ebd2..e74ee5b 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -30,7 +30,7 @@ #include #include /* for malloc(), free() */ #include /* for strlen(), memcmp(), memmove() */ -#include /* for localtime(), strftime() */ +#include /* for localtime_r(), strftime() */ #include /* for NAME_MAX */ #include /* for PRI formatting macro */ #include @@ -82,7 +82,7 @@ char *log_info[] = { "", "fatal", "error", "warn", "info", "debug", "verbose", " char *trace_type[] = { "", "variable", "func_in", "func_out", "state", "vfb", "", "", "", "", "", "", "", "", "", "" }; char *nw_trace_type[] = { "", "ipc", "can", "flexray", "most", "vfb", "", "", "", "", "", "", "", "", "", "" }; char *control_type[] = { "", "request", "response", "time", "", "", "", "", "", "", "", "", "", "", "", "" }; -static char *service_id[] = +static char *service_id_name[] = { "", "set_log_level", "set_trace_status", "get_log_info", "get_default_log_level", "store_config", "reset_to_factory_default", "set_com_interface_status", "set_com_interface_max_bandwidth", "set_verbose_mode", @@ -630,7 +630,7 @@ DltReturnValue dlt_message_header(DltMessage *msg, char *text, int textlength, i DltReturnValue dlt_message_header_flags(DltMessage *msg, char *text, int textlength, int flags, int verbose) { - struct tm *timeinfo; + struct tm timeinfo; char buffer [DLT_COMMON_BUFFER_LENGTH]; PRINT_FUNCTION_VERBOSE(verbose); @@ -646,12 +646,9 @@ DltReturnValue dlt_message_header_flags(DltMessage *msg, char *text, int textlen if ((flags & DLT_HEADER_SHOW_TIME) == DLT_HEADER_SHOW_TIME) { /* print received time */ time_t tt = msg->storageheader->seconds; - timeinfo = localtime (&tt); - - if (timeinfo != NULL) { - strftime (buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", timeinfo); - snprintf(text, textlength, "%s.%.6d ", buffer, msg->storageheader->microseconds); - } + localtime_r(&tt, &timeinfo); + strftime (buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", &timeinfo); + snprintf(text, textlength, "%s.%.6d ", buffer, msg->storageheader->microseconds); } if ((flags & DLT_HEADER_SHOW_TMSTP) == DLT_HEADER_SHOW_TMSTP) { @@ -822,7 +819,8 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, int textlength, /* process message id / service id */ if (DLT_MSG_IS_CONTROL(msg)) { if ((id > 0) && (id < DLT_SERVICE_ID_LAST_ENTRY)) { - snprintf(text + strlen(text), textlength - strlen(text), "%s", service_id[id]); /* service id */ + snprintf(text + strlen(text), textlength - strlen(text), "%s", + service_id_name[id]); /* service id */ } else if (!(DLT_MSG_IS_CONTROL_TIME(msg))) snprintf(text + strlen(text), textlength - strlen(text), "service(%u)", id); /* service id */ diff --git a/src/shared/dlt_offline_trace.c b/src/shared/dlt_offline_trace.c index 76de233..fd59fc0 100644 --- a/src/shared/dlt_offline_trace.c +++ b/src/shared/dlt_offline_trace.c @@ -178,7 +178,7 @@ unsigned int dlt_offline_trace_get_idx_of_log_file(char *file) DltReturnValue dlt_offline_trace_create_new_file(DltOfflineTrace *trace) { time_t t; - struct tm *tmp; + struct tm tmp; char outstr[200]; char newest[DLT_OFFLINETRACE_FILENAME_MAX_SIZE] = { 0 }; char oldest[DLT_OFFLINETRACE_FILENAME_MAX_SIZE] = { 0 }; @@ -188,14 +188,9 @@ DltReturnValue dlt_offline_trace_create_new_file(DltOfflineTrace *trace) if (trace->filenameTimestampBased) { int ret = 0; t = time(NULL); - tmp = localtime(&t); + localtime_r(&t, &tmp); - if (NULL == tmp) { - printf("dlt_offline_trace_create_new_file: pointer to tmp is NULL!"); - return DLT_RETURN_ERROR; - } - - if (strftime(outstr, sizeof(outstr), "%Y%m%d_%H%M%S", tmp) == 0) {} + strftime(outstr, sizeof(outstr), "%Y%m%d_%H%M%S", &tmp); ret = snprintf(trace->filename, NAME_MAX, "%s/dlt_offlinetrace_%s.dlt", trace->directory, outstr); diff --git a/src/system/dlt-system-journal.c b/src/system/dlt-system-journal.c index 055fbe6..0883945 100644 --- a/src/system/dlt-system-journal.c +++ b/src/system/dlt-system-journal.c @@ -166,7 +166,7 @@ void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *tim } time_secs = time_usecs / 1000000; - timeinfo = localtime((const time_t *)(&time_secs)); + localtime((const time_t *)(&time_secs), timeinfo); strftime(buffer_realtime_formatted, sizeof(buffer_realtime_formatted), "%Y/%m/%d %H:%M:%S", timeinfo); snprintf(timestamp->real, sizeof(timestamp->real), "%s.%06" PRIu64, buffer_realtime_formatted, -- cgit v1.2.1