From 96d3b1f9eecfd3431b67fac32db173caf9847fd1 Mon Sep 17 00:00:00 2001 From: JesterHodl Date: Sat, 14 Dec 2024 22:54:52 +0100 Subject: [PATCH] handle truncation in vsnprintf and reduce size of char time_buffer[] to 20 20 is sufficient --- src/datum_logger.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/datum_logger.c b/src/datum_logger.c index 8f825ba..62af06f 100644 --- a/src/datum_logger.c +++ b/src/datum_logger.c @@ -118,7 +118,7 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...) va_list args; struct timeval tv; struct tm tm_info; - char time_buffer[40]; + char time_buffer[20]; if ((level < log_level_console) && (level < log_level_file)) { return 0; @@ -201,8 +201,13 @@ int datum_logger_queue_msg(const char *func, int level, const char *format, ...) msg->calling_function[47] = 0; msg->msg = &msg_buffer[buffer_id][msg_buf_idx[buffer_id]]; va_start(args, format); - i = vsnprintf(msg->msg, sizeof(msg->msg), format, args); - msg->msg[i] = 0; + i = vsnprintf(msg->msg, 1023, format, args); + + // clamp i to actual written value in order not to waste buffer space + if (i >= 1023) { + i = 1022; + } + va_end(args); if (((msg_buf_idx[buffer_id]+i+2) > msg_buf_maxsz) || (dlog_queue_next[buffer_id] >= dlog_queue_max_entries)) { @@ -251,7 +256,7 @@ void * datum_logger_thread(void *ptr) { struct tm tm_info_storage; struct tm *tm_info; DLOG_MSG *msg; - char time_buffer[40]; + char time_buffer[20]; char log_line[1200]; FILE *log_handle = NULL; time_t next_log_rotate = get_midnight_timestamp();