Skip to content

Commit

Permalink
handle truncation in vsnprintf and reduce size of char time_buffer[] …
Browse files Browse the repository at this point in the history
…to 20

20 is sufficient
  • Loading branch information
jesterhodl committed Dec 14, 2024
1 parent b628b89 commit 96d3b1f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/datum_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 96d3b1f

Please sign in to comment.