From eedd8b9bfe3d33b3c4386433fbc56076391d7ba1 Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Thu, 20 Jun 2024 11:55:38 +0800 Subject: [PATCH] fix: malformed logs produced by `struct_utp_context::log()` --- utp_internal.cpp | 13 +++++++++---- utp_internal.h | 3 +++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/utp_internal.cpp b/utp_internal.cpp index f238939..fb5ace4 100644 --- a/utp_internal.cpp +++ b/utp_internal.cpp @@ -3388,21 +3388,26 @@ void struct_utp_context::log(int level, utp_socket *socket, char const *fmt, ... va_list va; va_start(va, fmt); - log_unchecked(socket, fmt, va); + log_impl(socket, fmt, va); va_end(va); } void struct_utp_context::log_unchecked(utp_socket *socket, char const *fmt, ...) { va_list va; + va_start(va, fmt); + log_impl(socket, fmt, va); + va_end(va); +} + +void struct_utp_context::log_impl(utp_socket *socket, char const *fmt, va_list va) +{ char buf[4096]; - va_start(va, fmt); vsnprintf(buf, 4096, fmt, va); buf[4095] = '\0'; - va_end(va); - utp_call_log(this, socket, (const byte *)buf); + utp_call_log(this, socket, reinterpret_cast(buf)); } inline bool struct_utp_context::would_log(int level) diff --git a/utp_internal.h b/utp_internal.h index 2cc7a9f..b5f92d5 100644 --- a/utp_internal.h +++ b/utp_internal.h @@ -182,6 +182,9 @@ struct struct_utp_context { bool log_normal:1; // log normal events? bool log_mtu:1; // log MTU related events? bool log_debug:1; // log debugging events? (Must also compile with UTP_DEBUG_LOGGING defined) + +private: + void log_impl(utp_socket *socket, char const *fmt, va_list va); }; #endif //__UTP_INTERNAL_H__